blob: b5e2b40cafcaa7718efcf8106e4e2cd79caf3bea [file] [log] [blame]
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001/*******************************************************************************
2 * Vhost kernel TCM fabric driver for virtio SCSI initiators
3 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07004 * (C) Copyright 2010-2013 Datera, Inc.
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07005 * (C) Copyright 2010-2012 IBM Corp.
6 *
7 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
8 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07009 * Authors: Nicholas A. Bellinger <nab@daterainc.com>
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070010 * 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>
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070048#include <linux/virtio_scsi.h>
Asias He9d6064a2013-01-06 14:36:13 +080049#include <linux/llist.h>
Asias He1b7f3902013-02-06 13:20:59 +080050#include <linux/bitmap.h>
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -070051#include <linux/percpu_ida.h>
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070052
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070053#include "vhost.h"
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +030054
55#define TCM_VHOST_VERSION "v0.1"
56#define TCM_VHOST_NAMELEN 256
57#define TCM_VHOST_MAX_CDB_SIZE 32
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -070058#define TCM_VHOST_DEFAULT_TAGS 256
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -070059#define TCM_VHOST_PREALLOC_SGLS 2048
Nicholas Bellinger5a01d082014-02-22 18:34:08 -080060#define TCM_VHOST_PREALLOC_UPAGES 2048
Nicholas Bellingerb1935f62014-02-22 18:08:24 -080061#define TCM_VHOST_PREALLOC_PROT_SGLS 512
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +030062
63struct vhost_scsi_inflight {
64 /* Wait for the flush operation to finish */
65 struct completion comp;
66 /* Refcount for the inflight reqs */
67 struct kref kref;
68};
69
70struct tcm_vhost_cmd {
71 /* Descriptor from vhost_get_vq_desc() for virt_queue segment */
72 int tvc_vq_desc;
73 /* virtio-scsi initiator task attribute */
74 int tvc_task_attr;
Nicholas Bellinger79c14142015-01-27 13:13:12 -080075 /* virtio-scsi response incoming iovecs */
76 int tvc_in_iovs;
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +030077 /* virtio-scsi initiator data direction */
78 enum dma_data_direction tvc_data_direction;
79 /* Expected data transfer length from virtio-scsi header */
80 u32 tvc_exp_data_len;
81 /* The Tag from include/linux/virtio_scsi.h:struct virtio_scsi_cmd_req */
82 u64 tvc_tag;
83 /* The number of scatterlists associated with this cmd */
84 u32 tvc_sgl_count;
Nicholas Bellingere31885d2014-02-22 18:34:43 -080085 u32 tvc_prot_sgl_count;
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +030086 /* Saved unpacked SCSI LUN for tcm_vhost_submission_work() */
87 u32 tvc_lun;
88 /* Pointer to the SGL formatted memory from virtio-scsi */
89 struct scatterlist *tvc_sgl;
Nicholas Bellingerb1935f62014-02-22 18:08:24 -080090 struct scatterlist *tvc_prot_sgl;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -070091 struct page **tvc_upages;
Nicholas Bellinger79c14142015-01-27 13:13:12 -080092 /* Pointer to response header iovec */
93 struct iovec *tvc_resp_iov;
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +030094 /* Pointer to vhost_scsi for our device */
95 struct vhost_scsi *tvc_vhost;
96 /* Pointer to vhost_virtqueue for the cmd */
97 struct vhost_virtqueue *tvc_vq;
98 /* Pointer to vhost nexus memory */
99 struct tcm_vhost_nexus *tvc_nexus;
100 /* The TCM I/O descriptor that is accessed via container_of() */
101 struct se_cmd tvc_se_cmd;
102 /* work item used for cmwq dispatch to tcm_vhost_submission_work() */
103 struct work_struct work;
104 /* Copy of the incoming SCSI command descriptor block (CDB) */
105 unsigned char tvc_cdb[TCM_VHOST_MAX_CDB_SIZE];
106 /* Sense buffer that will be mapped into outgoing status */
107 unsigned char tvc_sense_buf[TRANSPORT_SENSE_BUFFER];
108 /* Completed commands list, serviced from vhost worker thread */
109 struct llist_node tvc_completion_list;
110 /* Used to track inflight cmd */
111 struct vhost_scsi_inflight *inflight;
112};
113
114struct tcm_vhost_nexus {
115 /* Pointer to TCM session for I_T Nexus */
116 struct se_session *tvn_se_sess;
117};
118
119struct tcm_vhost_nacl {
120 /* Binary World Wide unique Port Name for Vhost Initiator port */
121 u64 iport_wwpn;
122 /* ASCII formatted WWPN for Sas Initiator port */
123 char iport_name[TCM_VHOST_NAMELEN];
124 /* Returned by tcm_vhost_make_nodeacl() */
125 struct se_node_acl se_node_acl;
126};
127
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300128struct tcm_vhost_tpg {
129 /* Vhost port target portal group tag for TCM */
130 u16 tport_tpgt;
131 /* Used to track number of TPG Port/Lun Links wrt to explict I_T Nexus shutdown */
132 int tv_tpg_port_count;
133 /* Used for vhost_scsi device reference to tpg_nexus, protected by tv_tpg_mutex */
134 int tv_tpg_vhost_count;
135 /* list for tcm_vhost_list */
136 struct list_head tv_tpg_list;
137 /* Used to protect access for tpg_nexus */
138 struct mutex tv_tpg_mutex;
139 /* Pointer to the TCM VHost I_T Nexus for this TPG endpoint */
140 struct tcm_vhost_nexus *tpg_nexus;
141 /* Pointer back to tcm_vhost_tport */
142 struct tcm_vhost_tport *tport;
143 /* Returned by tcm_vhost_make_tpg() */
144 struct se_portal_group se_tpg;
145 /* Pointer back to vhost_scsi, protected by tv_tpg_mutex */
146 struct vhost_scsi *vhost_scsi;
147};
148
149struct tcm_vhost_tport {
150 /* SCSI protocol the tport is providing */
151 u8 tport_proto_id;
152 /* Binary World Wide unique Port Name for Vhost Target port */
153 u64 tport_wwpn;
154 /* ASCII formatted WWPN for Vhost Target port */
155 char tport_name[TCM_VHOST_NAMELEN];
156 /* Returned by tcm_vhost_make_tport() */
157 struct se_wwn tport_wwn;
158};
159
160struct tcm_vhost_evt {
161 /* event to be sent to guest */
162 struct virtio_scsi_event event;
163 /* event list, serviced from vhost worker thread */
164 struct llist_node list;
165};
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700166
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700167enum {
168 VHOST_SCSI_VQ_CTL = 0,
169 VHOST_SCSI_VQ_EVT = 1,
170 VHOST_SCSI_VQ_IO = 2,
171};
172
Michael S. Tsirkine72fd722014-11-23 18:01:34 +0200173/* Note: can't set VIRTIO_F_VERSION_1 yet, since that implies ANY_LAYOUT. */
Nicholas Bellinger5dade712013-03-27 17:23:41 -0700174enum {
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800175 VHOST_SCSI_FEATURES = VHOST_FEATURES | (1ULL << VIRTIO_SCSI_F_HOTPLUG) |
Nicholas Bellinger664ed902015-01-28 01:31:52 -0800176 (1ULL << VIRTIO_SCSI_F_T10_PI) |
177 (1ULL << VIRTIO_F_ANY_LAYOUT) |
178 (1ULL << VIRTIO_F_VERSION_1)
Nicholas Bellinger5dade712013-03-27 17:23:41 -0700179};
180
Asias He1b7f3902013-02-06 13:20:59 +0800181#define VHOST_SCSI_MAX_TARGET 256
182#define VHOST_SCSI_MAX_VQ 128
Asias Hea6c9af82013-04-25 15:35:21 +0800183#define VHOST_SCSI_MAX_EVENT 128
Asias He67e18cf2013-02-05 12:31:57 +0800184
Asias He3ab2e422013-04-27 11:16:48 +0800185struct vhost_scsi_virtqueue {
186 struct vhost_virtqueue vq;
Michael S. Tsirkin3dfbff32013-04-28 15:38:52 +0300187 /*
188 * Reference counting for inflight reqs, used for flush operation. At
189 * each time, one reference tracks new commands submitted, while we
190 * wait for another one to reach 0.
191 */
Asias Hef2f0173d2013-04-27 11:16:49 +0800192 struct vhost_scsi_inflight inflights[2];
Michael S. Tsirkin3dfbff32013-04-28 15:38:52 +0300193 /*
194 * Indicate current inflight in use, protected by vq->mutex.
195 * Writers must also take dev mutex and flush under it.
196 */
Asias Hef2f0173d2013-04-27 11:16:49 +0800197 int inflight_idx;
Asias He3ab2e422013-04-27 11:16:48 +0800198};
199
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700200struct vhost_scsi {
Asias He67e18cf2013-02-05 12:31:57 +0800201 /* Protected by vhost_scsi->dev.mutex */
Asias He4f7f46d2013-04-03 14:17:37 +0800202 struct tcm_vhost_tpg **vs_tpg;
Asias He67e18cf2013-02-05 12:31:57 +0800203 char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
Asias He67e18cf2013-02-05 12:31:57 +0800204
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700205 struct vhost_dev dev;
Asias He3ab2e422013-04-27 11:16:48 +0800206 struct vhost_scsi_virtqueue vqs[VHOST_SCSI_MAX_VQ];
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700207
208 struct vhost_work vs_completion_work; /* cmd completion work item */
Asias He9d6064a2013-01-06 14:36:13 +0800209 struct llist_head vs_completion_list; /* cmd completion queue */
Asias Hea6c9af82013-04-25 15:35:21 +0800210
211 struct vhost_work vs_event_work; /* evt injection work item */
212 struct llist_head vs_event_list; /* evt injection queue */
213
214 bool vs_events_missed; /* any missed events, protected by vq->mutex */
215 int vs_events_nr; /* num of pending events, protected by vq->mutex */
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700216};
217
218/* Local pointer to allocated TCM configfs fabric module */
219static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
220
221static struct workqueue_struct *tcm_vhost_workqueue;
222
223/* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
224static DEFINE_MUTEX(tcm_vhost_mutex);
225static LIST_HEAD(tcm_vhost_list);
226
Nicholas Bellingerb4078b52015-01-28 13:10:51 -0800227static int iov_num_pages(void __user *iov_base, size_t iov_len)
Asias He765b34f2013-01-22 11:20:25 +0800228{
Nicholas Bellingerb4078b52015-01-28 13:10:51 -0800229 return (PAGE_ALIGN((unsigned long)iov_base + iov_len) -
230 ((unsigned long)iov_base & PAGE_MASK)) >> PAGE_SHIFT;
Asias He765b34f2013-01-22 11:20:25 +0800231}
232
Asias He0a1febf2013-06-05 21:17:38 +0800233static void tcm_vhost_done_inflight(struct kref *kref)
Asias Hef2f0173d2013-04-27 11:16:49 +0800234{
235 struct vhost_scsi_inflight *inflight;
236
237 inflight = container_of(kref, struct vhost_scsi_inflight, kref);
238 complete(&inflight->comp);
239}
240
241static void tcm_vhost_init_inflight(struct vhost_scsi *vs,
242 struct vhost_scsi_inflight *old_inflight[])
243{
244 struct vhost_scsi_inflight *new_inflight;
245 struct vhost_virtqueue *vq;
246 int idx, i;
247
248 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
249 vq = &vs->vqs[i].vq;
250
251 mutex_lock(&vq->mutex);
252
253 /* store old infight */
254 idx = vs->vqs[i].inflight_idx;
255 if (old_inflight)
256 old_inflight[i] = &vs->vqs[i].inflights[idx];
257
258 /* setup new infight */
259 vs->vqs[i].inflight_idx = idx ^ 1;
260 new_inflight = &vs->vqs[i].inflights[idx ^ 1];
261 kref_init(&new_inflight->kref);
262 init_completion(&new_inflight->comp);
263
264 mutex_unlock(&vq->mutex);
265 }
266}
267
268static struct vhost_scsi_inflight *
269tcm_vhost_get_inflight(struct vhost_virtqueue *vq)
270{
271 struct vhost_scsi_inflight *inflight;
272 struct vhost_scsi_virtqueue *svq;
273
274 svq = container_of(vq, struct vhost_scsi_virtqueue, vq);
275 inflight = &svq->inflights[svq->inflight_idx];
276 kref_get(&inflight->kref);
277
278 return inflight;
279}
280
281static void tcm_vhost_put_inflight(struct vhost_scsi_inflight *inflight)
282{
283 kref_put(&inflight->kref, tcm_vhost_done_inflight);
284}
285
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700286static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
287{
288 return 1;
289}
290
291static int tcm_vhost_check_false(struct se_portal_group *se_tpg)
292{
293 return 0;
294}
295
296static char *tcm_vhost_get_fabric_name(void)
297{
298 return "vhost";
299}
300
301static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg)
302{
303 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
304 struct tcm_vhost_tpg, se_tpg);
305 struct tcm_vhost_tport *tport = tpg->tport;
306
307 switch (tport->tport_proto_id) {
308 case SCSI_PROTOCOL_SAS:
309 return sas_get_fabric_proto_ident(se_tpg);
310 case SCSI_PROTOCOL_FCP:
311 return fc_get_fabric_proto_ident(se_tpg);
312 case SCSI_PROTOCOL_ISCSI:
313 return iscsi_get_fabric_proto_ident(se_tpg);
314 default:
315 pr_err("Unknown tport_proto_id: 0x%02x, using"
316 " SAS emulation\n", tport->tport_proto_id);
317 break;
318 }
319
320 return sas_get_fabric_proto_ident(se_tpg);
321}
322
323static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg)
324{
325 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
326 struct tcm_vhost_tpg, se_tpg);
327 struct tcm_vhost_tport *tport = tpg->tport;
328
329 return &tport->tport_name[0];
330}
331
332static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg)
333{
334 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
335 struct tcm_vhost_tpg, se_tpg);
336 return tpg->tport_tpgt;
337}
338
339static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
340{
341 return 1;
342}
343
Asias He683bd962013-05-06 16:38:27 +0800344static u32
345tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
346 struct se_node_acl *se_nacl,
347 struct t10_pr_registration *pr_reg,
348 int *format_code,
349 unsigned char *buf)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700350{
351 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
352 struct tcm_vhost_tpg, se_tpg);
353 struct tcm_vhost_tport *tport = tpg->tport;
354
355 switch (tport->tport_proto_id) {
356 case SCSI_PROTOCOL_SAS:
357 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
358 format_code, buf);
359 case SCSI_PROTOCOL_FCP:
360 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
361 format_code, buf);
362 case SCSI_PROTOCOL_ISCSI:
363 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
364 format_code, buf);
365 default:
366 pr_err("Unknown tport_proto_id: 0x%02x, using"
367 " SAS emulation\n", tport->tport_proto_id);
368 break;
369 }
370
371 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
372 format_code, buf);
373}
374
Asias He683bd962013-05-06 16:38:27 +0800375static u32
376tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
377 struct se_node_acl *se_nacl,
378 struct t10_pr_registration *pr_reg,
379 int *format_code)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700380{
381 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
382 struct tcm_vhost_tpg, se_tpg);
383 struct tcm_vhost_tport *tport = tpg->tport;
384
385 switch (tport->tport_proto_id) {
386 case SCSI_PROTOCOL_SAS:
387 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
388 format_code);
389 case SCSI_PROTOCOL_FCP:
390 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
391 format_code);
392 case SCSI_PROTOCOL_ISCSI:
393 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
394 format_code);
395 default:
396 pr_err("Unknown tport_proto_id: 0x%02x, using"
397 " SAS emulation\n", tport->tport_proto_id);
398 break;
399 }
400
401 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
402 format_code);
403}
404
Asias He683bd962013-05-06 16:38:27 +0800405static char *
406tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
407 const char *buf,
408 u32 *out_tid_len,
409 char **port_nexus_ptr)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700410{
411 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
412 struct tcm_vhost_tpg, se_tpg);
413 struct tcm_vhost_tport *tport = tpg->tport;
414
415 switch (tport->tport_proto_id) {
416 case SCSI_PROTOCOL_SAS:
417 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
418 port_nexus_ptr);
419 case SCSI_PROTOCOL_FCP:
420 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
421 port_nexus_ptr);
422 case SCSI_PROTOCOL_ISCSI:
423 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
424 port_nexus_ptr);
425 default:
426 pr_err("Unknown tport_proto_id: 0x%02x, using"
427 " SAS emulation\n", tport->tport_proto_id);
428 break;
429 }
430
431 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
432 port_nexus_ptr);
433}
434
Asias He683bd962013-05-06 16:38:27 +0800435static struct se_node_acl *
436tcm_vhost_alloc_fabric_acl(struct se_portal_group *se_tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700437{
438 struct tcm_vhost_nacl *nacl;
439
440 nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL);
441 if (!nacl) {
Masanari Iida744627e92012-11-05 23:30:40 +0900442 pr_err("Unable to allocate struct tcm_vhost_nacl\n");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700443 return NULL;
444 }
445
446 return &nacl->se_node_acl;
447}
448
Asias He683bd962013-05-06 16:38:27 +0800449static void
450tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
451 struct se_node_acl *se_nacl)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700452{
453 struct tcm_vhost_nacl *nacl = container_of(se_nacl,
454 struct tcm_vhost_nacl, se_node_acl);
455 kfree(nacl);
456}
457
458static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg)
459{
460 return 1;
461}
462
463static void tcm_vhost_release_cmd(struct se_cmd *se_cmd)
464{
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700465 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
466 struct tcm_vhost_cmd, tvc_se_cmd);
Nicholas Bellingerde1419e2015-01-29 17:21:13 -0800467 struct se_session *se_sess = tv_cmd->tvc_nexus->tvn_se_sess;
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800468 int i;
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700469
470 if (tv_cmd->tvc_sgl_count) {
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700471 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
472 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
Michael S. Tsirkind3d665a2013-09-17 22:54:31 +0300473 }
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800474 if (tv_cmd->tvc_prot_sgl_count) {
475 for (i = 0; i < tv_cmd->tvc_prot_sgl_count; i++)
476 put_page(sg_page(&tv_cmd->tvc_prot_sgl[i]));
477 }
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700478
479 tcm_vhost_put_inflight(tv_cmd->inflight);
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700480 percpu_ida_free(&se_sess->sess_tag_pool, se_cmd->map_tag);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700481}
482
483static int tcm_vhost_shutdown_session(struct se_session *se_sess)
484{
485 return 0;
486}
487
488static void tcm_vhost_close_session(struct se_session *se_sess)
489{
490 return;
491}
492
493static u32 tcm_vhost_sess_get_index(struct se_session *se_sess)
494{
495 return 0;
496}
497
498static int tcm_vhost_write_pending(struct se_cmd *se_cmd)
499{
500 /* Go ahead and process the write immediately */
501 target_execute_cmd(se_cmd);
502 return 0;
503}
504
505static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd)
506{
507 return 0;
508}
509
510static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl)
511{
512 return;
513}
514
515static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd)
516{
517 return 0;
518}
519
520static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
521{
522 return 0;
523}
524
Asias He3c63f662013-05-06 16:38:29 +0800525static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *cmd)
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700526{
Asias He3c63f662013-05-06 16:38:29 +0800527 struct vhost_scsi *vs = cmd->tvc_vhost;
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700528
Asias He3c63f662013-05-06 16:38:29 +0800529 llist_add(&cmd->tvc_completion_list, &vs->vs_completion_list);
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700530
531 vhost_work_queue(&vs->dev, &vs->vs_completion_work);
532}
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700533
534static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
535{
Asias He3c63f662013-05-06 16:38:29 +0800536 struct tcm_vhost_cmd *cmd = container_of(se_cmd,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700537 struct tcm_vhost_cmd, tvc_se_cmd);
Asias He3c63f662013-05-06 16:38:29 +0800538 vhost_scsi_complete_cmd(cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700539 return 0;
540}
541
542static int tcm_vhost_queue_status(struct se_cmd *se_cmd)
543{
Asias He3c63f662013-05-06 16:38:29 +0800544 struct tcm_vhost_cmd *cmd = container_of(se_cmd,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700545 struct tcm_vhost_cmd, tvc_se_cmd);
Asias He3c63f662013-05-06 16:38:29 +0800546 vhost_scsi_complete_cmd(cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700547 return 0;
548}
549
Joern Engelb79fafa2013-07-03 11:22:17 -0400550static void tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700551{
Joern Engelb79fafa2013-07-03 11:22:17 -0400552 return;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700553}
554
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700555static void tcm_vhost_aborted_task(struct se_cmd *se_cmd)
556{
557 return;
558}
559
Asias Hea6c9af82013-04-25 15:35:21 +0800560static void tcm_vhost_free_evt(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
561{
562 vs->vs_events_nr--;
563 kfree(evt);
564}
565
Asias He683bd962013-05-06 16:38:27 +0800566static struct tcm_vhost_evt *
567tcm_vhost_allocate_evt(struct vhost_scsi *vs,
568 u32 event, u32 reason)
Asias Hea6c9af82013-04-25 15:35:21 +0800569{
Asias He3ab2e422013-04-27 11:16:48 +0800570 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Asias Hea6c9af82013-04-25 15:35:21 +0800571 struct tcm_vhost_evt *evt;
572
573 if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) {
574 vs->vs_events_missed = true;
575 return NULL;
576 }
577
578 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
579 if (!evt) {
580 vq_err(vq, "Failed to allocate tcm_vhost_evt\n");
581 vs->vs_events_missed = true;
582 return NULL;
583 }
584
Michael S. Tsirkine72fd722014-11-23 18:01:34 +0200585 evt->event.event = cpu_to_vhost32(vq, event);
586 evt->event.reason = cpu_to_vhost32(vq, reason);
Asias Hea6c9af82013-04-25 15:35:21 +0800587 vs->vs_events_nr++;
588
589 return evt;
590}
591
Asias He3c63f662013-05-06 16:38:29 +0800592static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700593{
Asias He3c63f662013-05-06 16:38:29 +0800594 struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700595
596 /* TODO locking against target/backend threads? */
Nicholas Bellinger6c131d02013-06-06 01:44:48 -0700597 transport_generic_free_cmd(se_cmd, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700598
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700599}
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700600
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700601static int vhost_scsi_check_stop_free(struct se_cmd *se_cmd)
602{
603 return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700604}
605
Asias He683bd962013-05-06 16:38:27 +0800606static void
607tcm_vhost_do_evt_work(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
Asias Hea6c9af82013-04-25 15:35:21 +0800608{
Asias He3ab2e422013-04-27 11:16:48 +0800609 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Asias Hea6c9af82013-04-25 15:35:21 +0800610 struct virtio_scsi_event *event = &evt->event;
611 struct virtio_scsi_event __user *eventp;
612 unsigned out, in;
613 int head, ret;
614
615 if (!vq->private_data) {
616 vs->vs_events_missed = true;
617 return;
618 }
619
620again:
621 vhost_disable_notify(&vs->dev, vq);
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300622 head = vhost_get_vq_desc(vq, vq->iov,
Asias Hea6c9af82013-04-25 15:35:21 +0800623 ARRAY_SIZE(vq->iov), &out, &in,
624 NULL, NULL);
625 if (head < 0) {
626 vs->vs_events_missed = true;
627 return;
628 }
629 if (head == vq->num) {
630 if (vhost_enable_notify(&vs->dev, vq))
631 goto again;
632 vs->vs_events_missed = true;
633 return;
634 }
635
636 if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) {
637 vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n",
638 vq->iov[out].iov_len);
639 vs->vs_events_missed = true;
640 return;
641 }
642
643 if (vs->vs_events_missed) {
Michael S. Tsirkine72fd722014-11-23 18:01:34 +0200644 event->event |= cpu_to_vhost32(vq, VIRTIO_SCSI_T_EVENTS_MISSED);
Asias Hea6c9af82013-04-25 15:35:21 +0800645 vs->vs_events_missed = false;
646 }
647
648 eventp = vq->iov[out].iov_base;
649 ret = __copy_to_user(eventp, event, sizeof(*event));
650 if (!ret)
651 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
652 else
653 vq_err(vq, "Faulted on tcm_vhost_send_event\n");
654}
655
656static void tcm_vhost_evt_work(struct vhost_work *work)
657{
658 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
659 vs_event_work);
Asias He3ab2e422013-04-27 11:16:48 +0800660 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Asias Hea6c9af82013-04-25 15:35:21 +0800661 struct tcm_vhost_evt *evt;
662 struct llist_node *llnode;
663
664 mutex_lock(&vq->mutex);
665 llnode = llist_del_all(&vs->vs_event_list);
666 while (llnode) {
667 evt = llist_entry(llnode, struct tcm_vhost_evt, list);
668 llnode = llist_next(llnode);
669 tcm_vhost_do_evt_work(vs, evt);
670 tcm_vhost_free_evt(vs, evt);
671 }
672 mutex_unlock(&vq->mutex);
673}
674
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700675/* Fill in status and signal that we are done processing this command
676 *
677 * This is scheduled in the vhost work queue so we are called with the owner
678 * process mm and can access the vring.
679 */
680static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
681{
682 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
683 vs_completion_work);
Asias He1b7f3902013-02-06 13:20:59 +0800684 DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
Asias He9d6064a2013-01-06 14:36:13 +0800685 struct virtio_scsi_cmd_resp v_rsp;
Asias He3c63f662013-05-06 16:38:29 +0800686 struct tcm_vhost_cmd *cmd;
Asias He9d6064a2013-01-06 14:36:13 +0800687 struct llist_node *llnode;
688 struct se_cmd *se_cmd;
Nicholas Bellinger79c14142015-01-27 13:13:12 -0800689 struct iov_iter iov_iter;
Asias He1b7f3902013-02-06 13:20:59 +0800690 int ret, vq;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700691
Asias He1b7f3902013-02-06 13:20:59 +0800692 bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
Asias He9d6064a2013-01-06 14:36:13 +0800693 llnode = llist_del_all(&vs->vs_completion_list);
694 while (llnode) {
Asias He3c63f662013-05-06 16:38:29 +0800695 cmd = llist_entry(llnode, struct tcm_vhost_cmd,
Asias He9d6064a2013-01-06 14:36:13 +0800696 tvc_completion_list);
697 llnode = llist_next(llnode);
Asias He3c63f662013-05-06 16:38:29 +0800698 se_cmd = &cmd->tvc_se_cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700699
700 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
Asias He3c63f662013-05-06 16:38:29 +0800701 cmd, se_cmd->residual_count, se_cmd->scsi_status);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700702
703 memset(&v_rsp, 0, sizeof(v_rsp));
Michael S. Tsirkine72fd722014-11-23 18:01:34 +0200704 v_rsp.resid = cpu_to_vhost32(cmd->tvc_vq, se_cmd->residual_count);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700705 /* TODO is status_qualifier field needed? */
706 v_rsp.status = se_cmd->scsi_status;
Michael S. Tsirkine72fd722014-11-23 18:01:34 +0200707 v_rsp.sense_len = cpu_to_vhost32(cmd->tvc_vq,
708 se_cmd->scsi_sense_length);
Asias He3c63f662013-05-06 16:38:29 +0800709 memcpy(v_rsp.sense, cmd->tvc_sense_buf,
Michael S. Tsirkine72fd722014-11-23 18:01:34 +0200710 se_cmd->scsi_sense_length);
Nicholas Bellinger79c14142015-01-27 13:13:12 -0800711
712 iov_iter_init(&iov_iter, READ, cmd->tvc_resp_iov,
713 cmd->tvc_in_iovs, sizeof(v_rsp));
714 ret = copy_to_iter(&v_rsp, sizeof(v_rsp), &iov_iter);
715 if (likely(ret == sizeof(v_rsp))) {
Asias He3ab2e422013-04-27 11:16:48 +0800716 struct vhost_scsi_virtqueue *q;
Asias He3c63f662013-05-06 16:38:29 +0800717 vhost_add_used(cmd->tvc_vq, cmd->tvc_vq_desc, 0);
718 q = container_of(cmd->tvc_vq, struct vhost_scsi_virtqueue, vq);
Asias He3ab2e422013-04-27 11:16:48 +0800719 vq = q - vs->vqs;
Asias He1b7f3902013-02-06 13:20:59 +0800720 __set_bit(vq, signal);
721 } else
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700722 pr_err("Faulted on virtio_scsi_cmd_resp\n");
723
Asias He3c63f662013-05-06 16:38:29 +0800724 vhost_scsi_free_cmd(cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700725 }
726
Asias He1b7f3902013-02-06 13:20:59 +0800727 vq = -1;
728 while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1))
729 < VHOST_SCSI_MAX_VQ)
Asias He3ab2e422013-04-27 11:16:48 +0800730 vhost_signal(&vs->dev, &vs->vqs[vq].vq);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700731}
732
Asias He683bd962013-05-06 16:38:27 +0800733static struct tcm_vhost_cmd *
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800734vhost_scsi_get_tag(struct vhost_virtqueue *vq, struct tcm_vhost_tpg *tpg,
735 unsigned char *cdb, u64 scsi_tag, u16 lun, u8 task_attr,
736 u32 exp_data_len, int data_direction)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700737{
Asias He3c63f662013-05-06 16:38:29 +0800738 struct tcm_vhost_cmd *cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700739 struct tcm_vhost_nexus *tv_nexus;
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700740 struct se_session *se_sess;
Nicholas Bellingerb1935f62014-02-22 18:08:24 -0800741 struct scatterlist *sg, *prot_sg;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700742 struct page **pages;
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700743 int tag;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700744
Asias He98718312013-05-06 16:38:28 +0800745 tv_nexus = tpg->tpg_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700746 if (!tv_nexus) {
747 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
748 return ERR_PTR(-EIO);
749 }
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700750 se_sess = tv_nexus->tvn_se_sess;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700751
Kent Overstreet6f6b5d12014-01-19 08:26:37 +0000752 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
Nicholas Bellinger4a47d3a2013-09-23 11:42:28 -0700753 if (tag < 0) {
754 pr_err("Unable to obtain tag for tcm_vhost_cmd\n");
755 return ERR_PTR(-ENOMEM);
756 }
757
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700758 cmd = &((struct tcm_vhost_cmd *)se_sess->sess_cmd_map)[tag];
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700759 sg = cmd->tvc_sgl;
Nicholas Bellingerb1935f62014-02-22 18:08:24 -0800760 prot_sg = cmd->tvc_prot_sgl;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700761 pages = cmd->tvc_upages;
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700762 memset(cmd, 0, sizeof(struct tcm_vhost_cmd));
763
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700764 cmd->tvc_sgl = sg;
Nicholas Bellingerb1935f62014-02-22 18:08:24 -0800765 cmd->tvc_prot_sgl = prot_sg;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700766 cmd->tvc_upages = pages;
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700767 cmd->tvc_se_cmd.map_tag = tag;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800768 cmd->tvc_tag = scsi_tag;
769 cmd->tvc_lun = lun;
770 cmd->tvc_task_attr = task_attr;
Asias He3c63f662013-05-06 16:38:29 +0800771 cmd->tvc_exp_data_len = exp_data_len;
772 cmd->tvc_data_direction = data_direction;
773 cmd->tvc_nexus = tv_nexus;
774 cmd->inflight = tcm_vhost_get_inflight(vq);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700775
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800776 memcpy(cmd->tvc_cdb, cdb, TCM_VHOST_MAX_CDB_SIZE);
777
Asias He3c63f662013-05-06 16:38:29 +0800778 return cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700779}
780
781/*
782 * Map a user memory range into a scatterlist
783 *
784 * Returns the number of scatterlist entries used or -errno on error.
785 */
Asias He683bd962013-05-06 16:38:27 +0800786static int
Nicholas Bellingerb4078b52015-01-28 13:10:51 -0800787vhost_scsi_map_to_sgl(struct tcm_vhost_cmd *cmd,
788 void __user *ptr,
789 size_t len,
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700790 struct scatterlist *sgl,
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800791 bool write)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700792{
Nicholas Bellingerb4078b52015-01-28 13:10:51 -0800793 unsigned int npages = 0, offset, nbytes;
794 unsigned int pages_nr = iov_num_pages(ptr, len);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700795 struct scatterlist *sg = sgl;
Nicholas Bellingerb4078b52015-01-28 13:10:51 -0800796 struct page **pages = cmd->tvc_upages;
Asias He18100532013-01-22 11:20:27 +0800797 int ret, i;
798
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800799 if (pages_nr > TCM_VHOST_PREALLOC_UPAGES) {
800 pr_err("vhost_scsi_map_to_sgl() pages_nr: %u greater than"
801 " preallocated TCM_VHOST_PREALLOC_UPAGES: %u\n",
802 pages_nr, TCM_VHOST_PREALLOC_UPAGES);
803 return -ENOBUFS;
804 }
Asias He18100532013-01-22 11:20:27 +0800805
806 ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages);
807 /* No pages were pinned */
808 if (ret < 0)
809 goto out;
810 /* Less pages pinned than wanted */
811 if (ret != pages_nr) {
812 for (i = 0; i < ret; i++)
813 put_page(pages[i]);
814 ret = -EFAULT;
815 goto out;
816 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700817
818 while (len > 0) {
Asias He18100532013-01-22 11:20:27 +0800819 offset = (uintptr_t)ptr & ~PAGE_MASK;
820 nbytes = min_t(unsigned int, PAGE_SIZE - offset, len);
821 sg_set_page(sg, pages[npages], nbytes, offset);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700822 ptr += nbytes;
823 len -= nbytes;
824 sg++;
825 npages++;
826 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700827
Asias He18100532013-01-22 11:20:27 +0800828out:
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700829 return ret;
830}
831
Asias He683bd962013-05-06 16:38:27 +0800832static int
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800833vhost_scsi_calc_sgls(struct iov_iter *iter, size_t bytes, int max_sgls)
834{
835 int sgl_count = 0;
836
837 if (!iter || !iter->iov) {
838 pr_err("%s: iter->iov is NULL, but expected bytes: %zu"
839 " present\n", __func__, bytes);
840 return -EINVAL;
841 }
842
843 sgl_count = iov_iter_npages(iter, 0xffff);
844 if (sgl_count > max_sgls) {
845 pr_err("%s: requested sgl_count: %d exceeds pre-allocated"
846 " max_sgls: %d\n", __func__, sgl_count, max_sgls);
847 return -EINVAL;
848 }
849 return sgl_count;
850}
851
852static int
853vhost_scsi_iov_to_sgl(struct tcm_vhost_cmd *cmd, bool write,
854 struct iov_iter *iter, struct scatterlist *sg,
855 int sg_count)
856{
857 size_t off = iter->iov_offset;
858 int i, ret;
859
860 for (i = 0; i < iter->nr_segs; i++) {
861 void __user *base = iter->iov[i].iov_base + off;
862 size_t len = iter->iov[i].iov_len - off;
863
864 ret = vhost_scsi_map_to_sgl(cmd, base, len, sg, write);
865 if (ret < 0) {
866 for (i = 0; i < sg_count; i++) {
867 struct page *page = sg_page(&sg[i]);
868 if (page)
869 put_page(page);
870 }
871 return ret;
872 }
873 sg += ret;
874 off = 0;
875 }
876 return 0;
877}
878
879static int
880vhost_scsi_mapal(struct tcm_vhost_cmd *cmd,
881 size_t prot_bytes, struct iov_iter *prot_iter,
882 size_t data_bytes, struct iov_iter *data_iter)
883{
884 int sgl_count, ret;
885 bool write = (cmd->tvc_data_direction == DMA_FROM_DEVICE);
886
887 if (prot_bytes) {
888 sgl_count = vhost_scsi_calc_sgls(prot_iter, prot_bytes,
889 TCM_VHOST_PREALLOC_PROT_SGLS);
890 if (sgl_count < 0)
891 return sgl_count;
892
893 sg_init_table(cmd->tvc_prot_sgl, sgl_count);
894 cmd->tvc_prot_sgl_count = sgl_count;
895 pr_debug("%s prot_sg %p prot_sgl_count %u\n", __func__,
896 cmd->tvc_prot_sgl, cmd->tvc_prot_sgl_count);
897
898 ret = vhost_scsi_iov_to_sgl(cmd, write, prot_iter,
899 cmd->tvc_prot_sgl,
900 cmd->tvc_prot_sgl_count);
901 if (ret < 0) {
902 cmd->tvc_prot_sgl_count = 0;
903 return ret;
904 }
905 }
906 sgl_count = vhost_scsi_calc_sgls(data_iter, data_bytes,
907 TCM_VHOST_PREALLOC_SGLS);
908 if (sgl_count < 0)
909 return sgl_count;
910
911 sg_init_table(cmd->tvc_sgl, sgl_count);
912 cmd->tvc_sgl_count = sgl_count;
913 pr_debug("%s data_sg %p data_sgl_count %u\n", __func__,
914 cmd->tvc_sgl, cmd->tvc_sgl_count);
915
916 ret = vhost_scsi_iov_to_sgl(cmd, write, data_iter,
917 cmd->tvc_sgl, cmd->tvc_sgl_count);
918 if (ret < 0) {
919 cmd->tvc_sgl_count = 0;
920 return ret;
921 }
922 return 0;
923}
924
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700925static void tcm_vhost_submission_work(struct work_struct *work)
926{
Asias He3c63f662013-05-06 16:38:29 +0800927 struct tcm_vhost_cmd *cmd =
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700928 container_of(work, struct tcm_vhost_cmd, work);
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700929 struct tcm_vhost_nexus *tv_nexus;
Asias He3c63f662013-05-06 16:38:29 +0800930 struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800931 struct scatterlist *sg_ptr, *sg_prot_ptr = NULL;
932 int rc;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700933
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800934 /* FIXME: BIDI operation */
Asias He3c63f662013-05-06 16:38:29 +0800935 if (cmd->tvc_sgl_count) {
936 sg_ptr = cmd->tvc_sgl;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800937
938 if (cmd->tvc_prot_sgl_count)
939 sg_prot_ptr = cmd->tvc_prot_sgl;
940 else
941 se_cmd->prot_pto = true;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700942 } else {
943 sg_ptr = NULL;
944 }
Asias He3c63f662013-05-06 16:38:29 +0800945 tv_nexus = cmd->tvc_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700946
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700947 rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
Asias He3c63f662013-05-06 16:38:29 +0800948 cmd->tvc_cdb, &cmd->tvc_sense_buf[0],
949 cmd->tvc_lun, cmd->tvc_exp_data_len,
950 cmd->tvc_task_attr, cmd->tvc_data_direction,
Linus Torvalds6d2fa9e2013-07-11 12:57:19 -0700951 TARGET_SCF_ACK_KREF, sg_ptr, cmd->tvc_sgl_count,
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800952 NULL, 0, sg_prot_ptr, cmd->tvc_prot_sgl_count);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700953 if (rc < 0) {
954 transport_send_check_condition_and_sense(se_cmd,
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700955 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700956 transport_generic_free_cmd(se_cmd, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700957 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700958}
959
Asias He683bd962013-05-06 16:38:27 +0800960static void
961vhost_scsi_send_bad_target(struct vhost_scsi *vs,
962 struct vhost_virtqueue *vq,
963 int head, unsigned out)
Asias He637ab212013-04-10 15:06:15 +0800964{
965 struct virtio_scsi_cmd_resp __user *resp;
966 struct virtio_scsi_cmd_resp rsp;
967 int ret;
968
969 memset(&rsp, 0, sizeof(rsp));
970 rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
971 resp = vq->iov[out].iov_base;
972 ret = __copy_to_user(resp, &rsp, sizeof(rsp));
973 if (!ret)
974 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
975 else
976 pr_err("Faulted on virtio_scsi_cmd_resp\n");
977}
978
Asias He683bd962013-05-06 16:38:27 +0800979static void
980vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700981{
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -0800982 struct tcm_vhost_tpg **vs_tpg, *tpg;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700983 struct virtio_scsi_cmd_req v_req;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800984 struct virtio_scsi_cmd_req_pi v_req_pi;
Asias He3c63f662013-05-06 16:38:29 +0800985 struct tcm_vhost_cmd *cmd;
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -0800986 struct iov_iter out_iter, in_iter, prot_iter, data_iter;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800987 u64 tag;
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -0800988 u32 exp_data_len, data_direction;
989 unsigned out, in;
990 int head, ret, prot_bytes;
991 size_t req_size, rsp_size = sizeof(struct virtio_scsi_cmd_resp);
992 size_t out_size, in_size;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800993 u16 lun;
994 u8 *target, *lunp, task_attr;
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -0800995 bool t10_pi = vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI);
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800996 void *req, *cdb;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700997
Asias Hee7802212013-05-07 14:54:35 +0800998 mutex_lock(&vq->mutex);
Asias He4f7f46d2013-04-03 14:17:37 +0800999 /*
1000 * We can handle the vq only after the endpoint is setup by calling the
1001 * VHOST_SCSI_SET_ENDPOINT ioctl.
Asias He4f7f46d2013-04-03 14:17:37 +08001002 */
Asias Hee7802212013-05-07 14:54:35 +08001003 vs_tpg = vq->private_data;
Asias He4f7f46d2013-04-03 14:17:37 +08001004 if (!vs_tpg)
Asias Hee7802212013-05-07 14:54:35 +08001005 goto out;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001006
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001007 vhost_disable_notify(&vs->dev, vq);
1008
1009 for (;;) {
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001010 head = vhost_get_vq_desc(vq, vq->iov,
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001011 ARRAY_SIZE(vq->iov), &out, &in,
1012 NULL, NULL);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001013 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001014 head, out, in);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001015 /* On error, stop handling until the next kick. */
1016 if (unlikely(head < 0))
1017 break;
1018 /* Nothing new? Wait for eventfd to tell us they refilled. */
1019 if (head == vq->num) {
1020 if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
1021 vhost_disable_notify(&vs->dev, vq);
1022 continue;
1023 }
1024 break;
1025 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001026 /*
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001027 * Check for a sane response buffer so we can report early
1028 * errors back to the guest.
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001029 */
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001030 if (unlikely(vq->iov[out].iov_len < rsp_size)) {
1031 vq_err(vq, "Expecting at least virtio_scsi_cmd_resp"
1032 " size, got %zu bytes\n", vq->iov[out].iov_len);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001033 break;
1034 }
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001035 /*
1036 * Setup pointers and values based upon different virtio-scsi
1037 * request header if T10_PI is enabled in KVM guest.
1038 */
1039 if (t10_pi) {
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001040 req = &v_req_pi;
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001041 req_size = sizeof(v_req_pi);
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001042 lunp = &v_req_pi.lun[0];
1043 target = &v_req_pi.lun[1];
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001044 } else {
1045 req = &v_req;
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001046 req_size = sizeof(v_req);
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001047 lunp = &v_req.lun[0];
1048 target = &v_req.lun[1];
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001049 }
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001050 /*
1051 * FIXME: Not correct for BIDI operation
1052 */
1053 out_size = iov_length(vq->iov, out);
1054 in_size = iov_length(&vq->iov[out], in);
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001055
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001056 /*
1057 * Copy over the virtio-scsi request header, which for a
1058 * ANY_LAYOUT enabled guest may span multiple iovecs, or a
1059 * single iovec may contain both the header + outgoing
1060 * WRITE payloads.
1061 *
1062 * copy_from_iter() will advance out_iter, so that it will
1063 * point at the start of the outgoing WRITE payload, if
1064 * DMA_TO_DEVICE is set.
1065 */
1066 iov_iter_init(&out_iter, WRITE, vq->iov, out, out_size);
1067
1068 ret = copy_from_iter(req, req_size, &out_iter);
1069 if (unlikely(ret != req_size)) {
1070 vq_err(vq, "Faulted on copy_from_iter\n");
Nicholas Bellingerde1419e2015-01-29 17:21:13 -08001071 vhost_scsi_send_bad_target(vs, vq, head, out);
1072 continue;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001073 }
Venkatesh Srinivas7fe412d2014-02-24 14:13:32 -08001074 /* virtio-scsi spec requires byte 0 of the lun to be 1 */
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001075 if (unlikely(*lunp != 1)) {
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001076 vq_err(vq, "Illegal virtio-scsi lun: %u\n", *lunp);
Venkatesh Srinivas7fe412d2014-02-24 14:13:32 -08001077 vhost_scsi_send_bad_target(vs, vq, head, out);
1078 continue;
1079 }
1080
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001081 tpg = ACCESS_ONCE(vs_tpg[*target]);
Asias He98718312013-05-06 16:38:28 +08001082 if (unlikely(!tpg)) {
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001083 /* Target does not exist, fail the request */
Asias He637ab212013-04-10 15:06:15 +08001084 vhost_scsi_send_bad_target(vs, vq, head, out);
Asias He67e18cf2013-02-05 12:31:57 +08001085 continue;
1086 }
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001087 /*
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001088 * Determine data_direction by calculating the total outgoing
1089 * iovec sizes + incoming iovec sizes vs. virtio-scsi request +
1090 * response headers respectively.
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001091 *
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001092 * For DMA_TO_DEVICE this is out_iter, which is already pointing
1093 * to the right place.
1094 *
1095 * For DMA_FROM_DEVICE, the iovec will be just past the end
1096 * of the virtio-scsi response header in either the same
1097 * or immediately following iovec.
1098 *
1099 * Any associated T10_PI bytes for the outgoing / incoming
1100 * payloads are included in calculation of exp_data_len here.
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001101 */
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001102 prot_bytes = 0;
1103
1104 if (out_size > req_size) {
1105 data_direction = DMA_TO_DEVICE;
1106 exp_data_len = out_size - req_size;
1107 data_iter = out_iter;
1108 } else if (in_size > rsp_size) {
1109 data_direction = DMA_FROM_DEVICE;
1110 exp_data_len = in_size - rsp_size;
1111
1112 iov_iter_init(&in_iter, READ, &vq->iov[out], in,
1113 rsp_size + exp_data_len);
1114 iov_iter_advance(&in_iter, rsp_size);
1115 data_iter = in_iter;
1116 } else {
1117 data_direction = DMA_NONE;
1118 exp_data_len = 0;
1119 }
1120 /*
1121 * If T10_PI header + payload is present, setup prot_iter values
1122 * and recalculate data_iter for vhost_scsi_mapal() mapping to
1123 * host scatterlists via get_user_pages_fast().
1124 */
1125 if (t10_pi) {
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001126 if (v_req_pi.pi_bytesout) {
1127 if (data_direction != DMA_TO_DEVICE) {
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001128 vq_err(vq, "Received non zero pi_bytesout,"
1129 " but wrong data_direction\n");
Nicholas Bellingerde1419e2015-01-29 17:21:13 -08001130 vhost_scsi_send_bad_target(vs, vq, head, out);
1131 continue;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001132 }
Michael S. Tsirkine72fd722014-11-23 18:01:34 +02001133 prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesout);
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001134 } else if (v_req_pi.pi_bytesin) {
1135 if (data_direction != DMA_FROM_DEVICE) {
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001136 vq_err(vq, "Received non zero pi_bytesin,"
1137 " but wrong data_direction\n");
Nicholas Bellingerde1419e2015-01-29 17:21:13 -08001138 vhost_scsi_send_bad_target(vs, vq, head, out);
1139 continue;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001140 }
Michael S. Tsirkine72fd722014-11-23 18:01:34 +02001141 prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesin);
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001142 }
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001143 /*
1144 * Set prot_iter to data_iter, and advance past any
1145 * preceeding prot_bytes that may be present.
1146 *
1147 * Also fix up the exp_data_len to reflect only the
1148 * actual data payload length.
1149 */
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001150 if (prot_bytes) {
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001151 exp_data_len -= prot_bytes;
1152 prot_iter = data_iter;
1153 iov_iter_advance(&data_iter, prot_bytes);
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001154 }
Michael S. Tsirkine72fd722014-11-23 18:01:34 +02001155 tag = vhost64_to_cpu(vq, v_req_pi.tag);
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001156 task_attr = v_req_pi.task_attr;
1157 cdb = &v_req_pi.cdb[0];
1158 lun = ((v_req_pi.lun[2] << 8) | v_req_pi.lun[3]) & 0x3FFF;
1159 } else {
Michael S. Tsirkine72fd722014-11-23 18:01:34 +02001160 tag = vhost64_to_cpu(vq, v_req.tag);
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001161 task_attr = v_req.task_attr;
1162 cdb = &v_req.cdb[0];
1163 lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
1164 }
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001165 /*
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001166 * Check that the received CDB size does not exceeded our
1167 * hardcoded max for vhost-scsi, then get a pre-allocated
1168 * cmd descriptor for the new virtio-scsi tag.
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001169 *
1170 * TODO what if cdb was too small for varlen cdb header?
1171 */
1172 if (unlikely(scsi_command_size(cdb) > TCM_VHOST_MAX_CDB_SIZE)) {
1173 vq_err(vq, "Received SCSI CDB with command_size: %d that"
1174 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1175 scsi_command_size(cdb), TCM_VHOST_MAX_CDB_SIZE);
Nicholas Bellingerde1419e2015-01-29 17:21:13 -08001176 vhost_scsi_send_bad_target(vs, vq, head, out);
1177 continue;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001178 }
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001179 cmd = vhost_scsi_get_tag(vq, tpg, cdb, tag, lun, task_attr,
Nicholas Bellinger9f977ef2014-06-10 01:19:38 -07001180 exp_data_len + prot_bytes,
1181 data_direction);
Asias He3c63f662013-05-06 16:38:29 +08001182 if (IS_ERR(cmd)) {
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -07001183 vq_err(vq, "vhost_scsi_get_tag failed %ld\n",
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001184 PTR_ERR(cmd));
Nicholas Bellingerde1419e2015-01-29 17:21:13 -08001185 vhost_scsi_send_bad_target(vs, vq, head, out);
1186 continue;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001187 }
Asias He3c63f662013-05-06 16:38:29 +08001188 cmd->tvc_vhost = vs;
1189 cmd->tvc_vq = vq;
Nicholas Bellinger79c14142015-01-27 13:13:12 -08001190 cmd->tvc_resp_iov = &vq->iov[out];
1191 cmd->tvc_in_iovs = in;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001192
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001193 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001194 cmd->tvc_cdb[0], cmd->tvc_lun);
1195 pr_debug("cmd: %p exp_data_len: %d, prot_bytes: %d data_direction:"
1196 " %d\n", cmd, exp_data_len, prot_bytes, data_direction);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001197
1198 if (data_direction != DMA_NONE) {
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001199 ret = vhost_scsi_mapal(cmd,
1200 prot_bytes, &prot_iter,
1201 exp_data_len, &data_iter);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001202 if (unlikely(ret)) {
1203 vq_err(vq, "Failed to map iov to sgl\n");
Nicholas Bellingerde1419e2015-01-29 17:21:13 -08001204 tcm_vhost_release_cmd(&cmd->tvc_se_cmd);
1205 vhost_scsi_send_bad_target(vs, vq, head, out);
1206 continue;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001207 }
1208 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001209 /*
1210 * Save the descriptor from vhost_get_vq_desc() to be used to
1211 * complete the virtio-scsi request in TCM callback context via
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001212 * vhost_scsi_queue_data_in() and vhost_scsi_queue_status()
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001213 */
Asias He3c63f662013-05-06 16:38:29 +08001214 cmd->tvc_vq_desc = head;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001215 /*
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001216 * Dispatch cmd descriptor for cmwq execution in process
1217 * context provided by vhost_scsi_workqueue. This also ensures
1218 * cmd is executed on the same kworker CPU as this vhost
1219 * thread to gain positive L2 cache locality effects.
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001220 */
Asias He3c63f662013-05-06 16:38:29 +08001221 INIT_WORK(&cmd->work, tcm_vhost_submission_work);
1222 queue_work(tcm_vhost_workqueue, &cmd->work);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001223 }
Asias Hee7802212013-05-07 14:54:35 +08001224out:
Asias He7ea206c2013-04-10 15:06:14 +08001225 mutex_unlock(&vq->mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001226}
1227
1228static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
1229{
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001230 pr_debug("%s: The handling func for control queue.\n", __func__);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001231}
1232
Asias He683bd962013-05-06 16:38:27 +08001233static void
1234tcm_vhost_send_evt(struct vhost_scsi *vs,
1235 struct tcm_vhost_tpg *tpg,
1236 struct se_lun *lun,
1237 u32 event,
1238 u32 reason)
Asias Hea6c9af82013-04-25 15:35:21 +08001239{
1240 struct tcm_vhost_evt *evt;
1241
1242 evt = tcm_vhost_allocate_evt(vs, event, reason);
1243 if (!evt)
1244 return;
1245
1246 if (tpg && lun) {
1247 /* TODO: share lun setup code with virtio-scsi.ko */
1248 /*
1249 * Note: evt->event is zeroed when we allocate it and
1250 * lun[4-7] need to be zero according to virtio-scsi spec.
1251 */
1252 evt->event.lun[0] = 0x01;
1253 evt->event.lun[1] = tpg->tport_tpgt & 0xFF;
1254 if (lun->unpacked_lun >= 256)
1255 evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
1256 evt->event.lun[3] = lun->unpacked_lun & 0xFF;
1257 }
1258
1259 llist_add(&evt->list, &vs->vs_event_list);
1260 vhost_work_queue(&vs->dev, &vs->vs_event_work);
1261}
1262
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001263static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
1264{
Asias Hea6c9af82013-04-25 15:35:21 +08001265 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1266 poll.work);
1267 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1268
1269 mutex_lock(&vq->mutex);
1270 if (!vq->private_data)
1271 goto out;
1272
1273 if (vs->vs_events_missed)
1274 tcm_vhost_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
1275out:
1276 mutex_unlock(&vq->mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001277}
1278
1279static void vhost_scsi_handle_kick(struct vhost_work *work)
1280{
1281 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1282 poll.work);
1283 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1284
Asias He1b7f3902013-02-06 13:20:59 +08001285 vhost_scsi_handle_vq(vs, vq);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001286}
1287
Asias He4f7f46d2013-04-03 14:17:37 +08001288static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
1289{
Asias He3ab2e422013-04-27 11:16:48 +08001290 vhost_poll_flush(&vs->vqs[index].vq.poll);
Asias He4f7f46d2013-04-03 14:17:37 +08001291}
1292
Michael S. Tsirkin3dfbff32013-04-28 15:38:52 +03001293/* Callers must hold dev mutex */
Asias He4f7f46d2013-04-03 14:17:37 +08001294static void vhost_scsi_flush(struct vhost_scsi *vs)
1295{
Asias Hef2f0173d2013-04-27 11:16:49 +08001296 struct vhost_scsi_inflight *old_inflight[VHOST_SCSI_MAX_VQ];
Asias He4f7f46d2013-04-03 14:17:37 +08001297 int i;
1298
Asias Hef2f0173d2013-04-27 11:16:49 +08001299 /* Init new inflight and remember the old inflight */
1300 tcm_vhost_init_inflight(vs, old_inflight);
1301
1302 /*
1303 * The inflight->kref was initialized to 1. We decrement it here to
1304 * indicate the start of the flush operation so that it will reach 0
1305 * when all the reqs are finished.
1306 */
1307 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1308 kref_put(&old_inflight[i]->kref, tcm_vhost_done_inflight);
1309
1310 /* Flush both the vhost poll and vhost work */
Asias He4f7f46d2013-04-03 14:17:37 +08001311 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1312 vhost_scsi_flush_vq(vs, i);
1313 vhost_work_flush(&vs->dev, &vs->vs_completion_work);
Asias Hea6c9af82013-04-25 15:35:21 +08001314 vhost_work_flush(&vs->dev, &vs->vs_event_work);
Asias Hef2f0173d2013-04-27 11:16:49 +08001315
1316 /* Wait for all reqs issued before the flush to be finished */
1317 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1318 wait_for_completion(&old_inflight[i]->comp);
Asias He4f7f46d2013-04-03 14:17:37 +08001319}
1320
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001321/*
1322 * Called from vhost_scsi_ioctl() context to walk the list of available
1323 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
Asias Hef2b7daf2013-04-25 15:35:20 +08001324 *
1325 * The lock nesting rule is:
1326 * tcm_vhost_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001327 */
Asias He683bd962013-05-06 16:38:27 +08001328static int
1329vhost_scsi_set_endpoint(struct vhost_scsi *vs,
1330 struct vhost_scsi_target *t)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001331{
Nicholas Bellingerab8edab2014-10-08 06:19:20 +00001332 struct se_portal_group *se_tpg;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001333 struct tcm_vhost_tport *tv_tport;
Asias He98718312013-05-06 16:38:28 +08001334 struct tcm_vhost_tpg *tpg;
Asias He4f7f46d2013-04-03 14:17:37 +08001335 struct tcm_vhost_tpg **vs_tpg;
1336 struct vhost_virtqueue *vq;
1337 int index, ret, i, len;
Asias He67e18cf2013-02-05 12:31:57 +08001338 bool match = false;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001339
Asias Hef2b7daf2013-04-25 15:35:20 +08001340 mutex_lock(&tcm_vhost_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001341 mutex_lock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001342
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001343 /* Verify that ring has been setup correctly. */
1344 for (index = 0; index < vs->dev.nvqs; ++index) {
1345 /* Verify that ring has been setup correctly. */
Asias He3ab2e422013-04-27 11:16:48 +08001346 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
Asias Hef2b7daf2013-04-25 15:35:20 +08001347 ret = -EFAULT;
1348 goto out;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001349 }
1350 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001351
Asias He4f7f46d2013-04-03 14:17:37 +08001352 len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
1353 vs_tpg = kzalloc(len, GFP_KERNEL);
1354 if (!vs_tpg) {
Asias Hef2b7daf2013-04-25 15:35:20 +08001355 ret = -ENOMEM;
1356 goto out;
Asias He4f7f46d2013-04-03 14:17:37 +08001357 }
1358 if (vs->vs_tpg)
1359 memcpy(vs_tpg, vs->vs_tpg, len);
1360
Asias He98718312013-05-06 16:38:28 +08001361 list_for_each_entry(tpg, &tcm_vhost_list, tv_tpg_list) {
1362 mutex_lock(&tpg->tv_tpg_mutex);
1363 if (!tpg->tpg_nexus) {
1364 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001365 continue;
1366 }
Asias He98718312013-05-06 16:38:28 +08001367 if (tpg->tv_tpg_vhost_count != 0) {
1368 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001369 continue;
1370 }
Asias He98718312013-05-06 16:38:28 +08001371 tv_tport = tpg->tport;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001372
Asias He67e18cf2013-02-05 12:31:57 +08001373 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
Asias He98718312013-05-06 16:38:28 +08001374 if (vs->vs_tpg && vs->vs_tpg[tpg->tport_tpgt]) {
Asias He4f7f46d2013-04-03 14:17:37 +08001375 kfree(vs_tpg);
Asias He98718312013-05-06 16:38:28 +08001376 mutex_unlock(&tpg->tv_tpg_mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001377 ret = -EEXIST;
1378 goto out;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001379 }
Nicholas Bellingerab8edab2014-10-08 06:19:20 +00001380 /*
1381 * In order to ensure individual vhost-scsi configfs
1382 * groups cannot be removed while in use by vhost ioctl,
1383 * go ahead and take an explicit se_tpg->tpg_group.cg_item
1384 * dependency now.
1385 */
1386 se_tpg = &tpg->se_tpg;
1387 ret = configfs_depend_item(se_tpg->se_tpg_tfo->tf_subsys,
1388 &se_tpg->tpg_group.cg_item);
1389 if (ret) {
1390 pr_warn("configfs_depend_item() failed: %d\n", ret);
1391 kfree(vs_tpg);
1392 mutex_unlock(&tpg->tv_tpg_mutex);
1393 goto out;
1394 }
Asias He98718312013-05-06 16:38:28 +08001395 tpg->tv_tpg_vhost_count++;
1396 tpg->vhost_scsi = vs;
1397 vs_tpg[tpg->tport_tpgt] = tpg;
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001398 smp_mb__after_atomic();
Asias He67e18cf2013-02-05 12:31:57 +08001399 match = true;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001400 }
Asias He98718312013-05-06 16:38:28 +08001401 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001402 }
Asias He67e18cf2013-02-05 12:31:57 +08001403
1404 if (match) {
1405 memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
1406 sizeof(vs->vs_vhost_wwpn));
Asias He4f7f46d2013-04-03 14:17:37 +08001407 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
Asias He3ab2e422013-04-27 11:16:48 +08001408 vq = &vs->vqs[i].vq;
Asias He4f7f46d2013-04-03 14:17:37 +08001409 mutex_lock(&vq->mutex);
Asias He22fa90c2013-05-07 14:54:36 +08001410 vq->private_data = vs_tpg;
Asias Hedfd5d562013-04-03 14:17:38 +08001411 vhost_init_used(vq);
Asias He4f7f46d2013-04-03 14:17:37 +08001412 mutex_unlock(&vq->mutex);
1413 }
Asias He67e18cf2013-02-05 12:31:57 +08001414 ret = 0;
1415 } else {
1416 ret = -EEXIST;
1417 }
1418
Asias He4f7f46d2013-04-03 14:17:37 +08001419 /*
1420 * Act as synchronize_rcu to make sure access to
1421 * old vs->vs_tpg is finished.
1422 */
1423 vhost_scsi_flush(vs);
1424 kfree(vs->vs_tpg);
1425 vs->vs_tpg = vs_tpg;
1426
Asias Hef2b7daf2013-04-25 15:35:20 +08001427out:
Asias He67e18cf2013-02-05 12:31:57 +08001428 mutex_unlock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001429 mutex_unlock(&tcm_vhost_mutex);
Asias He67e18cf2013-02-05 12:31:57 +08001430 return ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001431}
1432
Asias He683bd962013-05-06 16:38:27 +08001433static int
1434vhost_scsi_clear_endpoint(struct vhost_scsi *vs,
1435 struct vhost_scsi_target *t)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001436{
Nicholas Bellingerab8edab2014-10-08 06:19:20 +00001437 struct se_portal_group *se_tpg;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001438 struct tcm_vhost_tport *tv_tport;
Asias He98718312013-05-06 16:38:28 +08001439 struct tcm_vhost_tpg *tpg;
Asias He4f7f46d2013-04-03 14:17:37 +08001440 struct vhost_virtqueue *vq;
1441 bool match = false;
Asias He67e18cf2013-02-05 12:31:57 +08001442 int index, ret, i;
1443 u8 target;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001444
Asias Hef2b7daf2013-04-25 15:35:20 +08001445 mutex_lock(&tcm_vhost_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001446 mutex_lock(&vs->dev.mutex);
1447 /* Verify that ring has been setup correctly. */
1448 for (index = 0; index < vs->dev.nvqs; ++index) {
Asias He3ab2e422013-04-27 11:16:48 +08001449 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001450 ret = -EFAULT;
Asias He038e0af2013-03-15 09:14:05 +08001451 goto err_dev;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001452 }
1453 }
Asias He4f7f46d2013-04-03 14:17:37 +08001454
1455 if (!vs->vs_tpg) {
Asias Hef2b7daf2013-04-25 15:35:20 +08001456 ret = 0;
1457 goto err_dev;
Asias He4f7f46d2013-04-03 14:17:37 +08001458 }
1459
Asias He67e18cf2013-02-05 12:31:57 +08001460 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
1461 target = i;
Asias He98718312013-05-06 16:38:28 +08001462 tpg = vs->vs_tpg[target];
1463 if (!tpg)
Asias He67e18cf2013-02-05 12:31:57 +08001464 continue;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001465
Asias He98718312013-05-06 16:38:28 +08001466 mutex_lock(&tpg->tv_tpg_mutex);
1467 tv_tport = tpg->tport;
Asias He67e18cf2013-02-05 12:31:57 +08001468 if (!tv_tport) {
1469 ret = -ENODEV;
Asias He038e0af2013-03-15 09:14:05 +08001470 goto err_tpg;
Asias He67e18cf2013-02-05 12:31:57 +08001471 }
1472
1473 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
Asias He98718312013-05-06 16:38:28 +08001474 pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu"
Asias He67e18cf2013-02-05 12:31:57 +08001475 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
Asias He98718312013-05-06 16:38:28 +08001476 tv_tport->tport_name, tpg->tport_tpgt,
Asias He67e18cf2013-02-05 12:31:57 +08001477 t->vhost_wwpn, t->vhost_tpgt);
1478 ret = -EINVAL;
Asias He038e0af2013-03-15 09:14:05 +08001479 goto err_tpg;
Asias He67e18cf2013-02-05 12:31:57 +08001480 }
Asias He98718312013-05-06 16:38:28 +08001481 tpg->tv_tpg_vhost_count--;
1482 tpg->vhost_scsi = NULL;
Asias He67e18cf2013-02-05 12:31:57 +08001483 vs->vs_tpg[target] = NULL;
Asias He4f7f46d2013-04-03 14:17:37 +08001484 match = true;
Asias He98718312013-05-06 16:38:28 +08001485 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellingerab8edab2014-10-08 06:19:20 +00001486 /*
1487 * Release se_tpg->tpg_group.cg_item configfs dependency now
1488 * to allow vhost-scsi WWPN se_tpg->tpg_group shutdown to occur.
1489 */
1490 se_tpg = &tpg->se_tpg;
1491 configfs_undepend_item(se_tpg->se_tpg_tfo->tf_subsys,
1492 &se_tpg->tpg_group.cg_item);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001493 }
Asias He4f7f46d2013-04-03 14:17:37 +08001494 if (match) {
1495 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
Asias He3ab2e422013-04-27 11:16:48 +08001496 vq = &vs->vqs[i].vq;
Asias He4f7f46d2013-04-03 14:17:37 +08001497 mutex_lock(&vq->mutex);
Asias He22fa90c2013-05-07 14:54:36 +08001498 vq->private_data = NULL;
Asias He4f7f46d2013-04-03 14:17:37 +08001499 mutex_unlock(&vq->mutex);
1500 }
1501 }
1502 /*
1503 * Act as synchronize_rcu to make sure access to
1504 * old vs->vs_tpg is finished.
1505 */
1506 vhost_scsi_flush(vs);
1507 kfree(vs->vs_tpg);
1508 vs->vs_tpg = NULL;
Asias Hea6c9af82013-04-25 15:35:21 +08001509 WARN_ON(vs->vs_events_nr);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001510 mutex_unlock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001511 mutex_unlock(&tcm_vhost_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001512 return 0;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001513
Asias He038e0af2013-03-15 09:14:05 +08001514err_tpg:
Asias He98718312013-05-06 16:38:28 +08001515 mutex_unlock(&tpg->tv_tpg_mutex);
Asias He038e0af2013-03-15 09:14:05 +08001516err_dev:
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001517 mutex_unlock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001518 mutex_unlock(&tcm_vhost_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001519 return ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001520}
1521
Asias He4f7f46d2013-04-03 14:17:37 +08001522static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
1523{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001524 struct vhost_virtqueue *vq;
1525 int i;
1526
Asias He4f7f46d2013-04-03 14:17:37 +08001527 if (features & ~VHOST_SCSI_FEATURES)
1528 return -EOPNOTSUPP;
1529
1530 mutex_lock(&vs->dev.mutex);
1531 if ((features & (1 << VHOST_F_LOG_ALL)) &&
1532 !vhost_log_access_ok(&vs->dev)) {
1533 mutex_unlock(&vs->dev.mutex);
1534 return -EFAULT;
1535 }
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001536
1537 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1538 vq = &vs->vqs[i].vq;
1539 mutex_lock(&vq->mutex);
1540 vq->acked_features = features;
1541 mutex_unlock(&vq->mutex);
1542 }
Asias He4f7f46d2013-04-03 14:17:37 +08001543 mutex_unlock(&vs->dev.mutex);
1544 return 0;
1545}
1546
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001547static int vhost_scsi_open(struct inode *inode, struct file *f)
1548{
Asias Hec7289312013-05-06 16:38:26 +08001549 struct vhost_scsi *vs;
Asias He3ab2e422013-04-27 11:16:48 +08001550 struct vhost_virtqueue **vqs;
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001551 int r = -ENOMEM, i;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001552
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001553 vs = kzalloc(sizeof(*vs), GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
1554 if (!vs) {
1555 vs = vzalloc(sizeof(*vs));
1556 if (!vs)
1557 goto err_vs;
1558 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001559
Asias He3ab2e422013-04-27 11:16:48 +08001560 vqs = kmalloc(VHOST_SCSI_MAX_VQ * sizeof(*vqs), GFP_KERNEL);
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001561 if (!vqs)
1562 goto err_vqs;
Asias He3ab2e422013-04-27 11:16:48 +08001563
Asias Hec7289312013-05-06 16:38:26 +08001564 vhost_work_init(&vs->vs_completion_work, vhost_scsi_complete_cmd_work);
1565 vhost_work_init(&vs->vs_event_work, tcm_vhost_evt_work);
Asias Hea6c9af82013-04-25 15:35:21 +08001566
Asias Hec7289312013-05-06 16:38:26 +08001567 vs->vs_events_nr = 0;
1568 vs->vs_events_missed = false;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001569
Asias Hec7289312013-05-06 16:38:26 +08001570 vqs[VHOST_SCSI_VQ_CTL] = &vs->vqs[VHOST_SCSI_VQ_CTL].vq;
1571 vqs[VHOST_SCSI_VQ_EVT] = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1572 vs->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick;
1573 vs->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick;
Asias He3ab2e422013-04-27 11:16:48 +08001574 for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) {
Asias Hec7289312013-05-06 16:38:26 +08001575 vqs[i] = &vs->vqs[i].vq;
1576 vs->vqs[i].vq.handle_kick = vhost_scsi_handle_kick;
Asias He3ab2e422013-04-27 11:16:48 +08001577 }
Zhi Yong Wu59566b6e2013-12-07 04:13:03 +08001578 vhost_dev_init(&vs->dev, vqs, VHOST_SCSI_MAX_VQ);
Asias Hef2f0173d2013-04-27 11:16:49 +08001579
Asias Hec7289312013-05-06 16:38:26 +08001580 tcm_vhost_init_inflight(vs, NULL);
Asias Hef2f0173d2013-04-27 11:16:49 +08001581
Asias Hec7289312013-05-06 16:38:26 +08001582 f->private_data = vs;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001583 return 0;
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001584
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001585err_vqs:
Michael S. Tsirkin68404442014-06-12 19:00:01 +03001586 kvfree(vs);
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001587err_vs:
1588 return r;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001589}
1590
1591static int vhost_scsi_release(struct inode *inode, struct file *f)
1592{
Asias Hec7289312013-05-06 16:38:26 +08001593 struct vhost_scsi *vs = f->private_data;
Asias He67e18cf2013-02-05 12:31:57 +08001594 struct vhost_scsi_target t;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001595
Asias Hec7289312013-05-06 16:38:26 +08001596 mutex_lock(&vs->dev.mutex);
1597 memcpy(t.vhost_wwpn, vs->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
1598 mutex_unlock(&vs->dev.mutex);
1599 vhost_scsi_clear_endpoint(vs, &t);
1600 vhost_dev_stop(&vs->dev);
1601 vhost_dev_cleanup(&vs->dev, false);
Asias Hea6c9af82013-04-25 15:35:21 +08001602 /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
Asias Hec7289312013-05-06 16:38:26 +08001603 vhost_scsi_flush(vs);
1604 kfree(vs->dev.vqs);
Michael S. Tsirkin68404442014-06-12 19:00:01 +03001605 kvfree(vs);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001606 return 0;
1607}
1608
Asias He683bd962013-05-06 16:38:27 +08001609static long
1610vhost_scsi_ioctl(struct file *f,
1611 unsigned int ioctl,
1612 unsigned long arg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001613{
1614 struct vhost_scsi *vs = f->private_data;
1615 struct vhost_scsi_target backend;
1616 void __user *argp = (void __user *)arg;
1617 u64 __user *featurep = argp;
Asias He11c63412013-04-25 15:35:22 +08001618 u32 __user *eventsp = argp;
1619 u32 events_missed;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001620 u64 features;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001621 int r, abi_version = VHOST_SCSI_ABI_VERSION;
Asias He3ab2e422013-04-27 11:16:48 +08001622 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001623
1624 switch (ioctl) {
1625 case VHOST_SCSI_SET_ENDPOINT:
1626 if (copy_from_user(&backend, argp, sizeof backend))
1627 return -EFAULT;
Michael S. Tsirkin6de71452012-08-18 15:44:09 -07001628 if (backend.reserved != 0)
1629 return -EOPNOTSUPP;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001630
1631 return vhost_scsi_set_endpoint(vs, &backend);
1632 case VHOST_SCSI_CLEAR_ENDPOINT:
1633 if (copy_from_user(&backend, argp, sizeof backend))
1634 return -EFAULT;
Michael S. Tsirkin6de71452012-08-18 15:44:09 -07001635 if (backend.reserved != 0)
1636 return -EOPNOTSUPP;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001637
1638 return vhost_scsi_clear_endpoint(vs, &backend);
1639 case VHOST_SCSI_GET_ABI_VERSION:
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001640 if (copy_to_user(argp, &abi_version, sizeof abi_version))
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001641 return -EFAULT;
1642 return 0;
Asias He11c63412013-04-25 15:35:22 +08001643 case VHOST_SCSI_SET_EVENTS_MISSED:
1644 if (get_user(events_missed, eventsp))
1645 return -EFAULT;
1646 mutex_lock(&vq->mutex);
1647 vs->vs_events_missed = events_missed;
1648 mutex_unlock(&vq->mutex);
1649 return 0;
1650 case VHOST_SCSI_GET_EVENTS_MISSED:
1651 mutex_lock(&vq->mutex);
1652 events_missed = vs->vs_events_missed;
1653 mutex_unlock(&vq->mutex);
1654 if (put_user(events_missed, eventsp))
1655 return -EFAULT;
1656 return 0;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001657 case VHOST_GET_FEATURES:
Nicholas Bellinger5dade712013-03-27 17:23:41 -07001658 features = VHOST_SCSI_FEATURES;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001659 if (copy_to_user(featurep, &features, sizeof features))
1660 return -EFAULT;
1661 return 0;
1662 case VHOST_SET_FEATURES:
1663 if (copy_from_user(&features, featurep, sizeof features))
1664 return -EFAULT;
1665 return vhost_scsi_set_features(vs, features);
1666 default:
1667 mutex_lock(&vs->dev.mutex);
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001668 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
1669 /* TODO: flush backend after dev ioctl. */
1670 if (r == -ENOIOCTLCMD)
1671 r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001672 mutex_unlock(&vs->dev.mutex);
1673 return r;
1674 }
1675}
1676
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001677#ifdef CONFIG_COMPAT
1678static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
1679 unsigned long arg)
1680{
1681 return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1682}
1683#endif
1684
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001685static const struct file_operations vhost_scsi_fops = {
1686 .owner = THIS_MODULE,
1687 .release = vhost_scsi_release,
1688 .unlocked_ioctl = vhost_scsi_ioctl,
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001689#ifdef CONFIG_COMPAT
1690 .compat_ioctl = vhost_scsi_compat_ioctl,
1691#endif
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001692 .open = vhost_scsi_open,
1693 .llseek = noop_llseek,
1694};
1695
1696static struct miscdevice vhost_scsi_misc = {
1697 MISC_DYNAMIC_MINOR,
1698 "vhost-scsi",
1699 &vhost_scsi_fops,
1700};
1701
1702static int __init vhost_scsi_register(void)
1703{
1704 return misc_register(&vhost_scsi_misc);
1705}
1706
1707static int vhost_scsi_deregister(void)
1708{
1709 return misc_deregister(&vhost_scsi_misc);
1710}
1711
1712static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
1713{
1714 switch (tport->tport_proto_id) {
1715 case SCSI_PROTOCOL_SAS:
1716 return "SAS";
1717 case SCSI_PROTOCOL_FCP:
1718 return "FCP";
1719 case SCSI_PROTOCOL_ISCSI:
1720 return "iSCSI";
1721 default:
1722 break;
1723 }
1724
1725 return "Unknown";
1726}
1727
Asias He683bd962013-05-06 16:38:27 +08001728static void
1729tcm_vhost_do_plug(struct tcm_vhost_tpg *tpg,
1730 struct se_lun *lun, bool plug)
Asias Hea6c9af82013-04-25 15:35:21 +08001731{
1732
1733 struct vhost_scsi *vs = tpg->vhost_scsi;
1734 struct vhost_virtqueue *vq;
1735 u32 reason;
1736
1737 if (!vs)
1738 return;
1739
1740 mutex_lock(&vs->dev.mutex);
Asias Hea6c9af82013-04-25 15:35:21 +08001741
1742 if (plug)
1743 reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
1744 else
1745 reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
1746
Asias He3ab2e422013-04-27 11:16:48 +08001747 vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Asias Hea6c9af82013-04-25 15:35:21 +08001748 mutex_lock(&vq->mutex);
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001749 if (vhost_has_feature(vq, VIRTIO_SCSI_F_HOTPLUG))
1750 tcm_vhost_send_evt(vs, tpg, lun,
1751 VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
Asias Hea6c9af82013-04-25 15:35:21 +08001752 mutex_unlock(&vq->mutex);
1753 mutex_unlock(&vs->dev.mutex);
1754}
1755
1756static void tcm_vhost_hotplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1757{
1758 tcm_vhost_do_plug(tpg, lun, true);
1759}
1760
1761static void tcm_vhost_hotunplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1762{
1763 tcm_vhost_do_plug(tpg, lun, false);
1764}
1765
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001766static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08001767 struct se_lun *lun)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001768{
Asias He98718312013-05-06 16:38:28 +08001769 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001770 struct tcm_vhost_tpg, se_tpg);
1771
Asias Hea6c9af82013-04-25 15:35:21 +08001772 mutex_lock(&tcm_vhost_mutex);
1773
Asias He98718312013-05-06 16:38:28 +08001774 mutex_lock(&tpg->tv_tpg_mutex);
1775 tpg->tv_tpg_port_count++;
1776 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001777
Asias He98718312013-05-06 16:38:28 +08001778 tcm_vhost_hotplug(tpg, lun);
Asias Hea6c9af82013-04-25 15:35:21 +08001779
1780 mutex_unlock(&tcm_vhost_mutex);
1781
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001782 return 0;
1783}
1784
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001785static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08001786 struct se_lun *lun)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001787{
Asias He98718312013-05-06 16:38:28 +08001788 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001789 struct tcm_vhost_tpg, se_tpg);
1790
Asias Hea6c9af82013-04-25 15:35:21 +08001791 mutex_lock(&tcm_vhost_mutex);
1792
Asias He98718312013-05-06 16:38:28 +08001793 mutex_lock(&tpg->tv_tpg_mutex);
1794 tpg->tv_tpg_port_count--;
1795 mutex_unlock(&tpg->tv_tpg_mutex);
Asias Hea6c9af82013-04-25 15:35:21 +08001796
Asias He98718312013-05-06 16:38:28 +08001797 tcm_vhost_hotunplug(tpg, lun);
Asias Hea6c9af82013-04-25 15:35:21 +08001798
1799 mutex_unlock(&tcm_vhost_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001800}
1801
Asias He683bd962013-05-06 16:38:27 +08001802static struct se_node_acl *
1803tcm_vhost_make_nodeacl(struct se_portal_group *se_tpg,
1804 struct config_group *group,
1805 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001806{
1807 struct se_node_acl *se_nacl, *se_nacl_new;
1808 struct tcm_vhost_nacl *nacl;
1809 u64 wwpn = 0;
1810 u32 nexus_depth;
1811
1812 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1813 return ERR_PTR(-EINVAL); */
1814 se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
1815 if (!se_nacl_new)
1816 return ERR_PTR(-ENOMEM);
1817
1818 nexus_depth = 1;
1819 /*
1820 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1821 * when converting a NodeACL from demo mode -> explict
1822 */
1823 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1824 name, nexus_depth);
1825 if (IS_ERR(se_nacl)) {
1826 tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
1827 return se_nacl;
1828 }
1829 /*
1830 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1831 */
1832 nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
1833 nacl->iport_wwpn = wwpn;
1834
1835 return se_nacl;
1836}
1837
1838static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
1839{
1840 struct tcm_vhost_nacl *nacl = container_of(se_acl,
1841 struct tcm_vhost_nacl, se_node_acl);
1842 core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1843 kfree(nacl);
1844}
1845
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001846static void tcm_vhost_free_cmd_map_res(struct tcm_vhost_nexus *nexus,
1847 struct se_session *se_sess)
1848{
1849 struct tcm_vhost_cmd *tv_cmd;
1850 unsigned int i;
1851
1852 if (!se_sess->sess_cmd_map)
1853 return;
1854
1855 for (i = 0; i < TCM_VHOST_DEFAULT_TAGS; i++) {
1856 tv_cmd = &((struct tcm_vhost_cmd *)se_sess->sess_cmd_map)[i];
1857
1858 kfree(tv_cmd->tvc_sgl);
Nicholas Bellingerb1935f62014-02-22 18:08:24 -08001859 kfree(tv_cmd->tvc_prot_sgl);
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001860 kfree(tv_cmd->tvc_upages);
1861 }
1862}
1863
Asias He98718312013-05-06 16:38:28 +08001864static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tpg,
Asias He683bd962013-05-06 16:38:27 +08001865 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001866{
1867 struct se_portal_group *se_tpg;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001868 struct se_session *se_sess;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001869 struct tcm_vhost_nexus *tv_nexus;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001870 struct tcm_vhost_cmd *tv_cmd;
1871 unsigned int i;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001872
Asias He98718312013-05-06 16:38:28 +08001873 mutex_lock(&tpg->tv_tpg_mutex);
1874 if (tpg->tpg_nexus) {
1875 mutex_unlock(&tpg->tv_tpg_mutex);
1876 pr_debug("tpg->tpg_nexus already exists\n");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001877 return -EEXIST;
1878 }
Asias He98718312013-05-06 16:38:28 +08001879 se_tpg = &tpg->se_tpg;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001880
1881 tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
1882 if (!tv_nexus) {
Asias He98718312013-05-06 16:38:28 +08001883 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001884 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1885 return -ENOMEM;
1886 }
1887 /*
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -07001888 * Initialize the struct se_session pointer and setup tagpool
1889 * for struct tcm_vhost_cmd descriptors
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001890 */
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -07001891 tv_nexus->tvn_se_sess = transport_init_session_tags(
1892 TCM_VHOST_DEFAULT_TAGS,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07001893 sizeof(struct tcm_vhost_cmd),
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001894 TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001895 if (IS_ERR(tv_nexus->tvn_se_sess)) {
Asias He98718312013-05-06 16:38:28 +08001896 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001897 kfree(tv_nexus);
1898 return -ENOMEM;
1899 }
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001900 se_sess = tv_nexus->tvn_se_sess;
1901 for (i = 0; i < TCM_VHOST_DEFAULT_TAGS; i++) {
1902 tv_cmd = &((struct tcm_vhost_cmd *)se_sess->sess_cmd_map)[i];
1903
1904 tv_cmd->tvc_sgl = kzalloc(sizeof(struct scatterlist) *
1905 TCM_VHOST_PREALLOC_SGLS, GFP_KERNEL);
1906 if (!tv_cmd->tvc_sgl) {
1907 mutex_unlock(&tpg->tv_tpg_mutex);
1908 pr_err("Unable to allocate tv_cmd->tvc_sgl\n");
1909 goto out;
1910 }
1911
1912 tv_cmd->tvc_upages = kzalloc(sizeof(struct page *) *
Nicholas Bellinger5a01d082014-02-22 18:34:08 -08001913 TCM_VHOST_PREALLOC_UPAGES, GFP_KERNEL);
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001914 if (!tv_cmd->tvc_upages) {
1915 mutex_unlock(&tpg->tv_tpg_mutex);
1916 pr_err("Unable to allocate tv_cmd->tvc_upages\n");
1917 goto out;
1918 }
Nicholas Bellingerb1935f62014-02-22 18:08:24 -08001919
1920 tv_cmd->tvc_prot_sgl = kzalloc(sizeof(struct scatterlist) *
1921 TCM_VHOST_PREALLOC_PROT_SGLS, GFP_KERNEL);
1922 if (!tv_cmd->tvc_prot_sgl) {
1923 mutex_unlock(&tpg->tv_tpg_mutex);
1924 pr_err("Unable to allocate tv_cmd->tvc_prot_sgl\n");
1925 goto out;
1926 }
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001927 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001928 /*
1929 * Since we are running in 'demo mode' this call with generate a
1930 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1931 * the SCSI Initiator port name of the passed configfs group 'name'.
1932 */
1933 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1934 se_tpg, (unsigned char *)name);
1935 if (!tv_nexus->tvn_se_sess->se_node_acl) {
Asias He98718312013-05-06 16:38:28 +08001936 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001937 pr_debug("core_tpg_check_initiator_node_acl() failed"
1938 " for %s\n", name);
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001939 goto out;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001940 }
1941 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001942 * Now register the TCM vhost virtual I_T Nexus as active with the
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001943 * call to __transport_register_session()
1944 */
1945 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1946 tv_nexus->tvn_se_sess, tv_nexus);
Asias He98718312013-05-06 16:38:28 +08001947 tpg->tpg_nexus = tv_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001948
Asias He98718312013-05-06 16:38:28 +08001949 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001950 return 0;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001951
1952out:
1953 tcm_vhost_free_cmd_map_res(tv_nexus, se_sess);
1954 transport_free_session(se_sess);
1955 kfree(tv_nexus);
1956 return -ENOMEM;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001957}
1958
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001959static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001960{
1961 struct se_session *se_sess;
1962 struct tcm_vhost_nexus *tv_nexus;
1963
1964 mutex_lock(&tpg->tv_tpg_mutex);
1965 tv_nexus = tpg->tpg_nexus;
1966 if (!tv_nexus) {
1967 mutex_unlock(&tpg->tv_tpg_mutex);
1968 return -ENODEV;
1969 }
1970
1971 se_sess = tv_nexus->tvn_se_sess;
1972 if (!se_sess) {
1973 mutex_unlock(&tpg->tv_tpg_mutex);
1974 return -ENODEV;
1975 }
1976
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001977 if (tpg->tv_tpg_port_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001978 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001979 pr_err("Unable to remove TCM_vhost I_T Nexus with"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001980 " active TPG port count: %d\n",
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001981 tpg->tv_tpg_port_count);
1982 return -EBUSY;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001983 }
1984
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001985 if (tpg->tv_tpg_vhost_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001986 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001987 pr_err("Unable to remove TCM_vhost I_T Nexus with"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001988 " active TPG vhost count: %d\n",
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001989 tpg->tv_tpg_vhost_count);
1990 return -EBUSY;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001991 }
1992
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001993 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001994 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
1995 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001996
1997 tcm_vhost_free_cmd_map_res(tv_nexus, se_sess);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001998 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001999 * Release the SCSI I_T Nexus to the emulated vhost Target Port
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002000 */
2001 transport_deregister_session(tv_nexus->tvn_se_sess);
2002 tpg->tpg_nexus = NULL;
2003 mutex_unlock(&tpg->tv_tpg_mutex);
2004
2005 kfree(tv_nexus);
2006 return 0;
2007}
2008
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002009static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08002010 char *page)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002011{
Asias He98718312013-05-06 16:38:28 +08002012 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002013 struct tcm_vhost_tpg, se_tpg);
2014 struct tcm_vhost_nexus *tv_nexus;
2015 ssize_t ret;
2016
Asias He98718312013-05-06 16:38:28 +08002017 mutex_lock(&tpg->tv_tpg_mutex);
2018 tv_nexus = tpg->tpg_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002019 if (!tv_nexus) {
Asias He98718312013-05-06 16:38:28 +08002020 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002021 return -ENODEV;
2022 }
2023 ret = snprintf(page, PAGE_SIZE, "%s\n",
2024 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
Asias He98718312013-05-06 16:38:28 +08002025 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002026
2027 return ret;
2028}
2029
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002030static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08002031 const char *page,
2032 size_t count)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002033{
Asias He98718312013-05-06 16:38:28 +08002034 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002035 struct tcm_vhost_tpg, se_tpg);
Asias He98718312013-05-06 16:38:28 +08002036 struct tcm_vhost_tport *tport_wwn = tpg->tport;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002037 unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
2038 int ret;
2039 /*
2040 * Shutdown the active I_T nexus if 'NULL' is passed..
2041 */
2042 if (!strncmp(page, "NULL", 4)) {
Asias He98718312013-05-06 16:38:28 +08002043 ret = tcm_vhost_drop_nexus(tpg);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002044 return (!ret) ? count : ret;
2045 }
2046 /*
2047 * Otherwise make sure the passed virtual Initiator port WWN matches
2048 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
2049 * tcm_vhost_make_nexus().
2050 */
2051 if (strlen(page) >= TCM_VHOST_NAMELEN) {
2052 pr_err("Emulated NAA Sas Address: %s, exceeds"
2053 " max: %d\n", page, TCM_VHOST_NAMELEN);
2054 return -EINVAL;
2055 }
2056 snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
2057
2058 ptr = strstr(i_port, "naa.");
2059 if (ptr) {
2060 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
2061 pr_err("Passed SAS Initiator Port %s does not"
2062 " match target port protoid: %s\n", i_port,
2063 tcm_vhost_dump_proto_id(tport_wwn));
2064 return -EINVAL;
2065 }
2066 port_ptr = &i_port[0];
2067 goto check_newline;
2068 }
2069 ptr = strstr(i_port, "fc.");
2070 if (ptr) {
2071 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
2072 pr_err("Passed FCP Initiator Port %s does not"
2073 " match target port protoid: %s\n", i_port,
2074 tcm_vhost_dump_proto_id(tport_wwn));
2075 return -EINVAL;
2076 }
2077 port_ptr = &i_port[3]; /* Skip over "fc." */
2078 goto check_newline;
2079 }
2080 ptr = strstr(i_port, "iqn.");
2081 if (ptr) {
2082 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
2083 pr_err("Passed iSCSI Initiator Port %s does not"
2084 " match target port protoid: %s\n", i_port,
2085 tcm_vhost_dump_proto_id(tport_wwn));
2086 return -EINVAL;
2087 }
2088 port_ptr = &i_port[0];
2089 goto check_newline;
2090 }
2091 pr_err("Unable to locate prefix for emulated Initiator Port:"
2092 " %s\n", i_port);
2093 return -EINVAL;
2094 /*
2095 * Clear any trailing newline for the NAA WWN
2096 */
2097check_newline:
2098 if (i_port[strlen(i_port)-1] == '\n')
2099 i_port[strlen(i_port)-1] = '\0';
2100
Asias He98718312013-05-06 16:38:28 +08002101 ret = tcm_vhost_make_nexus(tpg, port_ptr);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002102 if (ret < 0)
2103 return ret;
2104
2105 return count;
2106}
2107
2108TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
2109
2110static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
2111 &tcm_vhost_tpg_nexus.attr,
2112 NULL,
2113};
2114
Asias He683bd962013-05-06 16:38:27 +08002115static struct se_portal_group *
2116tcm_vhost_make_tpg(struct se_wwn *wwn,
2117 struct config_group *group,
2118 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002119{
2120 struct tcm_vhost_tport *tport = container_of(wwn,
2121 struct tcm_vhost_tport, tport_wwn);
2122
2123 struct tcm_vhost_tpg *tpg;
2124 unsigned long tpgt;
2125 int ret;
2126
2127 if (strstr(name, "tpgt_") != name)
2128 return ERR_PTR(-EINVAL);
2129 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
2130 return ERR_PTR(-EINVAL);
2131
2132 tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
2133 if (!tpg) {
2134 pr_err("Unable to allocate struct tcm_vhost_tpg");
2135 return ERR_PTR(-ENOMEM);
2136 }
2137 mutex_init(&tpg->tv_tpg_mutex);
2138 INIT_LIST_HEAD(&tpg->tv_tpg_list);
2139 tpg->tport = tport;
2140 tpg->tport_tpgt = tpgt;
2141
2142 ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
2143 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
2144 if (ret < 0) {
2145 kfree(tpg);
2146 return NULL;
2147 }
2148 mutex_lock(&tcm_vhost_mutex);
2149 list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
2150 mutex_unlock(&tcm_vhost_mutex);
2151
2152 return &tpg->se_tpg;
2153}
2154
2155static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
2156{
2157 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
2158 struct tcm_vhost_tpg, se_tpg);
2159
2160 mutex_lock(&tcm_vhost_mutex);
2161 list_del(&tpg->tv_tpg_list);
2162 mutex_unlock(&tcm_vhost_mutex);
2163 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002164 * Release the virtual I_T Nexus for this vhost TPG
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002165 */
2166 tcm_vhost_drop_nexus(tpg);
2167 /*
2168 * Deregister the se_tpg from TCM..
2169 */
2170 core_tpg_deregister(se_tpg);
2171 kfree(tpg);
2172}
2173
Asias He683bd962013-05-06 16:38:27 +08002174static struct se_wwn *
2175tcm_vhost_make_tport(struct target_fabric_configfs *tf,
2176 struct config_group *group,
2177 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002178{
2179 struct tcm_vhost_tport *tport;
2180 char *ptr;
2181 u64 wwpn = 0;
2182 int off = 0;
2183
2184 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
2185 return ERR_PTR(-EINVAL); */
2186
2187 tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
2188 if (!tport) {
2189 pr_err("Unable to allocate struct tcm_vhost_tport");
2190 return ERR_PTR(-ENOMEM);
2191 }
2192 tport->tport_wwpn = wwpn;
2193 /*
2194 * Determine the emulated Protocol Identifier and Target Port Name
2195 * based on the incoming configfs directory name.
2196 */
2197 ptr = strstr(name, "naa.");
2198 if (ptr) {
2199 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
2200 goto check_len;
2201 }
2202 ptr = strstr(name, "fc.");
2203 if (ptr) {
2204 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
2205 off = 3; /* Skip over "fc." */
2206 goto check_len;
2207 }
2208 ptr = strstr(name, "iqn.");
2209 if (ptr) {
2210 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
2211 goto check_len;
2212 }
2213
2214 pr_err("Unable to locate prefix for emulated Target Port:"
2215 " %s\n", name);
2216 kfree(tport);
2217 return ERR_PTR(-EINVAL);
2218
2219check_len:
2220 if (strlen(name) >= TCM_VHOST_NAMELEN) {
2221 pr_err("Emulated %s Address: %s, exceeds"
2222 " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
2223 TCM_VHOST_NAMELEN);
2224 kfree(tport);
2225 return ERR_PTR(-EINVAL);
2226 }
2227 snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
2228
2229 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
2230 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
2231
2232 return &tport->tport_wwn;
2233}
2234
2235static void tcm_vhost_drop_tport(struct se_wwn *wwn)
2236{
2237 struct tcm_vhost_tport *tport = container_of(wwn,
2238 struct tcm_vhost_tport, tport_wwn);
2239
2240 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
2241 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
2242 tport->tport_name);
2243
2244 kfree(tport);
2245}
2246
Asias He683bd962013-05-06 16:38:27 +08002247static ssize_t
2248tcm_vhost_wwn_show_attr_version(struct target_fabric_configfs *tf,
2249 char *page)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002250{
2251 return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
2252 "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
2253 utsname()->machine);
2254}
2255
2256TF_WWN_ATTR_RO(tcm_vhost, version);
2257
2258static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
2259 &tcm_vhost_wwn_version.attr,
2260 NULL,
2261};
2262
2263static struct target_core_fabric_ops tcm_vhost_ops = {
2264 .get_fabric_name = tcm_vhost_get_fabric_name,
2265 .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
2266 .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
2267 .tpg_get_tag = tcm_vhost_get_tag,
2268 .tpg_get_default_depth = tcm_vhost_get_default_depth,
2269 .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
2270 .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
2271 .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
2272 .tpg_check_demo_mode = tcm_vhost_check_true,
2273 .tpg_check_demo_mode_cache = tcm_vhost_check_true,
2274 .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
2275 .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
2276 .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
2277 .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
2278 .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
2279 .release_cmd = tcm_vhost_release_cmd,
Nicholas Bellinger084ed452013-06-06 02:20:41 -07002280 .check_stop_free = vhost_scsi_check_stop_free,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002281 .shutdown_session = tcm_vhost_shutdown_session,
2282 .close_session = tcm_vhost_close_session,
2283 .sess_get_index = tcm_vhost_sess_get_index,
2284 .sess_get_initiator_sid = NULL,
2285 .write_pending = tcm_vhost_write_pending,
2286 .write_pending_status = tcm_vhost_write_pending_status,
2287 .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
2288 .get_task_tag = tcm_vhost_get_task_tag,
2289 .get_cmd_state = tcm_vhost_get_cmd_state,
2290 .queue_data_in = tcm_vhost_queue_data_in,
2291 .queue_status = tcm_vhost_queue_status,
2292 .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002293 .aborted_task = tcm_vhost_aborted_task,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002294 /*
2295 * Setup callers for generic logic in target_core_fabric_configfs.c
2296 */
2297 .fabric_make_wwn = tcm_vhost_make_tport,
2298 .fabric_drop_wwn = tcm_vhost_drop_tport,
2299 .fabric_make_tpg = tcm_vhost_make_tpg,
2300 .fabric_drop_tpg = tcm_vhost_drop_tpg,
2301 .fabric_post_link = tcm_vhost_port_link,
2302 .fabric_pre_unlink = tcm_vhost_port_unlink,
2303 .fabric_make_np = NULL,
2304 .fabric_drop_np = NULL,
2305 .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
2306 .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
2307};
2308
2309static int tcm_vhost_register_configfs(void)
2310{
2311 struct target_fabric_configfs *fabric;
2312 int ret;
2313
2314 pr_debug("TCM_VHOST fabric module %s on %s/%s"
2315 " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
2316 utsname()->machine);
2317 /*
2318 * Register the top level struct config_item_type with TCM core
2319 */
2320 fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
2321 if (IS_ERR(fabric)) {
2322 pr_err("target_fabric_configfs_init() failed\n");
2323 return PTR_ERR(fabric);
2324 }
2325 /*
2326 * Setup fabric->tf_ops from our local tcm_vhost_ops
2327 */
2328 fabric->tf_ops = tcm_vhost_ops;
2329 /*
2330 * Setup default attribute lists for various fabric->tf_cit_tmpl
2331 */
Andy Groverd80e224d2013-10-09 11:05:56 -07002332 fabric->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
2333 fabric->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
2334 fabric->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = NULL;
2335 fabric->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL;
2336 fabric->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
2337 fabric->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL;
2338 fabric->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
2339 fabric->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
2340 fabric->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002341 /*
2342 * Register the fabric for use within TCM
2343 */
2344 ret = target_fabric_configfs_register(fabric);
2345 if (ret < 0) {
2346 pr_err("target_fabric_configfs_register() failed"
2347 " for TCM_VHOST\n");
2348 return ret;
2349 }
2350 /*
2351 * Setup our local pointer to *fabric
2352 */
2353 tcm_vhost_fabric_configfs = fabric;
2354 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
2355 return 0;
2356};
2357
2358static void tcm_vhost_deregister_configfs(void)
2359{
2360 if (!tcm_vhost_fabric_configfs)
2361 return;
2362
2363 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
2364 tcm_vhost_fabric_configfs = NULL;
2365 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
2366};
2367
2368static int __init tcm_vhost_init(void)
2369{
2370 int ret = -ENOMEM;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002371 /*
2372 * Use our own dedicated workqueue for submitting I/O into
2373 * target core to avoid contention within system_wq.
2374 */
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002375 tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
2376 if (!tcm_vhost_workqueue)
2377 goto out;
2378
2379 ret = vhost_scsi_register();
2380 if (ret < 0)
2381 goto out_destroy_workqueue;
2382
2383 ret = tcm_vhost_register_configfs();
2384 if (ret < 0)
2385 goto out_vhost_scsi_deregister;
2386
2387 return 0;
2388
2389out_vhost_scsi_deregister:
2390 vhost_scsi_deregister();
2391out_destroy_workqueue:
2392 destroy_workqueue(tcm_vhost_workqueue);
2393out:
2394 return ret;
2395};
2396
2397static void tcm_vhost_exit(void)
2398{
2399 tcm_vhost_deregister_configfs();
2400 vhost_scsi_deregister();
2401 destroy_workqueue(tcm_vhost_workqueue);
2402};
2403
Michael S. Tsirkin181c04a2013-05-02 03:52:59 +03002404MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
2405MODULE_ALIAS("tcm_vhost");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002406MODULE_LICENSE("GPL");
2407module_init(tcm_vhost_init);
2408module_exit(tcm_vhost_exit);