blob: 01c01cb3933fdfc5bce7e9c457d128d2e8f460e4 [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;
75 /* virtio-scsi initiator data direction */
76 enum dma_data_direction tvc_data_direction;
77 /* Expected data transfer length from virtio-scsi header */
78 u32 tvc_exp_data_len;
79 /* The Tag from include/linux/virtio_scsi.h:struct virtio_scsi_cmd_req */
80 u64 tvc_tag;
81 /* The number of scatterlists associated with this cmd */
82 u32 tvc_sgl_count;
Nicholas Bellingere31885d2014-02-22 18:34:43 -080083 u32 tvc_prot_sgl_count;
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +030084 /* Saved unpacked SCSI LUN for tcm_vhost_submission_work() */
85 u32 tvc_lun;
86 /* Pointer to the SGL formatted memory from virtio-scsi */
87 struct scatterlist *tvc_sgl;
Nicholas Bellingerb1935f62014-02-22 18:08:24 -080088 struct scatterlist *tvc_prot_sgl;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -070089 struct page **tvc_upages;
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +030090 /* Pointer to response */
91 struct virtio_scsi_cmd_resp __user *tvc_resp;
92 /* Pointer to vhost_scsi for our device */
93 struct vhost_scsi *tvc_vhost;
94 /* Pointer to vhost_virtqueue for the cmd */
95 struct vhost_virtqueue *tvc_vq;
96 /* Pointer to vhost nexus memory */
97 struct tcm_vhost_nexus *tvc_nexus;
98 /* The TCM I/O descriptor that is accessed via container_of() */
99 struct se_cmd tvc_se_cmd;
100 /* work item used for cmwq dispatch to tcm_vhost_submission_work() */
101 struct work_struct work;
102 /* Copy of the incoming SCSI command descriptor block (CDB) */
103 unsigned char tvc_cdb[TCM_VHOST_MAX_CDB_SIZE];
104 /* Sense buffer that will be mapped into outgoing status */
105 unsigned char tvc_sense_buf[TRANSPORT_SENSE_BUFFER];
106 /* Completed commands list, serviced from vhost worker thread */
107 struct llist_node tvc_completion_list;
108 /* Used to track inflight cmd */
109 struct vhost_scsi_inflight *inflight;
110};
111
112struct tcm_vhost_nexus {
113 /* Pointer to TCM session for I_T Nexus */
114 struct se_session *tvn_se_sess;
115};
116
117struct tcm_vhost_nacl {
118 /* Binary World Wide unique Port Name for Vhost Initiator port */
119 u64 iport_wwpn;
120 /* ASCII formatted WWPN for Sas Initiator port */
121 char iport_name[TCM_VHOST_NAMELEN];
122 /* Returned by tcm_vhost_make_nodeacl() */
123 struct se_node_acl se_node_acl;
124};
125
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300126struct tcm_vhost_tpg {
127 /* Vhost port target portal group tag for TCM */
128 u16 tport_tpgt;
129 /* Used to track number of TPG Port/Lun Links wrt to explict I_T Nexus shutdown */
130 int tv_tpg_port_count;
131 /* Used for vhost_scsi device reference to tpg_nexus, protected by tv_tpg_mutex */
132 int tv_tpg_vhost_count;
133 /* list for tcm_vhost_list */
134 struct list_head tv_tpg_list;
135 /* Used to protect access for tpg_nexus */
136 struct mutex tv_tpg_mutex;
137 /* Pointer to the TCM VHost I_T Nexus for this TPG endpoint */
138 struct tcm_vhost_nexus *tpg_nexus;
139 /* Pointer back to tcm_vhost_tport */
140 struct tcm_vhost_tport *tport;
141 /* Returned by tcm_vhost_make_tpg() */
142 struct se_portal_group se_tpg;
143 /* Pointer back to vhost_scsi, protected by tv_tpg_mutex */
144 struct vhost_scsi *vhost_scsi;
145};
146
147struct tcm_vhost_tport {
148 /* SCSI protocol the tport is providing */
149 u8 tport_proto_id;
150 /* Binary World Wide unique Port Name for Vhost Target port */
151 u64 tport_wwpn;
152 /* ASCII formatted WWPN for Vhost Target port */
153 char tport_name[TCM_VHOST_NAMELEN];
154 /* Returned by tcm_vhost_make_tport() */
155 struct se_wwn tport_wwn;
156};
157
158struct tcm_vhost_evt {
159 /* event to be sent to guest */
160 struct virtio_scsi_event event;
161 /* event list, serviced from vhost worker thread */
162 struct llist_node list;
163};
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700164
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700165enum {
166 VHOST_SCSI_VQ_CTL = 0,
167 VHOST_SCSI_VQ_EVT = 1,
168 VHOST_SCSI_VQ_IO = 2,
169};
170
Michael S. Tsirkine72fd722014-11-23 18:01:34 +0200171/* Note: can't set VIRTIO_F_VERSION_1 yet, since that implies ANY_LAYOUT. */
Nicholas Bellinger5dade712013-03-27 17:23:41 -0700172enum {
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800173 VHOST_SCSI_FEATURES = VHOST_FEATURES | (1ULL << VIRTIO_SCSI_F_HOTPLUG) |
174 (1ULL << VIRTIO_SCSI_F_T10_PI)
Nicholas Bellinger5dade712013-03-27 17:23:41 -0700175};
176
Asias He1b7f3902013-02-06 13:20:59 +0800177#define VHOST_SCSI_MAX_TARGET 256
178#define VHOST_SCSI_MAX_VQ 128
Asias Hea6c9af82013-04-25 15:35:21 +0800179#define VHOST_SCSI_MAX_EVENT 128
Asias He67e18cf2013-02-05 12:31:57 +0800180
Asias He3ab2e422013-04-27 11:16:48 +0800181struct vhost_scsi_virtqueue {
182 struct vhost_virtqueue vq;
Michael S. Tsirkin3dfbff32013-04-28 15:38:52 +0300183 /*
184 * Reference counting for inflight reqs, used for flush operation. At
185 * each time, one reference tracks new commands submitted, while we
186 * wait for another one to reach 0.
187 */
Asias Hef2f0173d2013-04-27 11:16:49 +0800188 struct vhost_scsi_inflight inflights[2];
Michael S. Tsirkin3dfbff32013-04-28 15:38:52 +0300189 /*
190 * Indicate current inflight in use, protected by vq->mutex.
191 * Writers must also take dev mutex and flush under it.
192 */
Asias Hef2f0173d2013-04-27 11:16:49 +0800193 int inflight_idx;
Asias He3ab2e422013-04-27 11:16:48 +0800194};
195
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700196struct vhost_scsi {
Asias He67e18cf2013-02-05 12:31:57 +0800197 /* Protected by vhost_scsi->dev.mutex */
Asias He4f7f46d2013-04-03 14:17:37 +0800198 struct tcm_vhost_tpg **vs_tpg;
Asias He67e18cf2013-02-05 12:31:57 +0800199 char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
Asias He67e18cf2013-02-05 12:31:57 +0800200
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700201 struct vhost_dev dev;
Asias He3ab2e422013-04-27 11:16:48 +0800202 struct vhost_scsi_virtqueue vqs[VHOST_SCSI_MAX_VQ];
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700203
204 struct vhost_work vs_completion_work; /* cmd completion work item */
Asias He9d6064a2013-01-06 14:36:13 +0800205 struct llist_head vs_completion_list; /* cmd completion queue */
Asias Hea6c9af82013-04-25 15:35:21 +0800206
207 struct vhost_work vs_event_work; /* evt injection work item */
208 struct llist_head vs_event_list; /* evt injection queue */
209
210 bool vs_events_missed; /* any missed events, protected by vq->mutex */
211 int vs_events_nr; /* num of pending events, protected by vq->mutex */
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700212};
213
214/* Local pointer to allocated TCM configfs fabric module */
215static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
216
217static struct workqueue_struct *tcm_vhost_workqueue;
218
219/* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
220static DEFINE_MUTEX(tcm_vhost_mutex);
221static LIST_HEAD(tcm_vhost_list);
222
Asias He765b34f2013-01-22 11:20:25 +0800223static int iov_num_pages(struct iovec *iov)
224{
225 return (PAGE_ALIGN((unsigned long)iov->iov_base + iov->iov_len) -
226 ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
227}
228
Asias He0a1febf2013-06-05 21:17:38 +0800229static void tcm_vhost_done_inflight(struct kref *kref)
Asias Hef2f0173d2013-04-27 11:16:49 +0800230{
231 struct vhost_scsi_inflight *inflight;
232
233 inflight = container_of(kref, struct vhost_scsi_inflight, kref);
234 complete(&inflight->comp);
235}
236
237static void tcm_vhost_init_inflight(struct vhost_scsi *vs,
238 struct vhost_scsi_inflight *old_inflight[])
239{
240 struct vhost_scsi_inflight *new_inflight;
241 struct vhost_virtqueue *vq;
242 int idx, i;
243
244 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
245 vq = &vs->vqs[i].vq;
246
247 mutex_lock(&vq->mutex);
248
249 /* store old infight */
250 idx = vs->vqs[i].inflight_idx;
251 if (old_inflight)
252 old_inflight[i] = &vs->vqs[i].inflights[idx];
253
254 /* setup new infight */
255 vs->vqs[i].inflight_idx = idx ^ 1;
256 new_inflight = &vs->vqs[i].inflights[idx ^ 1];
257 kref_init(&new_inflight->kref);
258 init_completion(&new_inflight->comp);
259
260 mutex_unlock(&vq->mutex);
261 }
262}
263
264static struct vhost_scsi_inflight *
265tcm_vhost_get_inflight(struct vhost_virtqueue *vq)
266{
267 struct vhost_scsi_inflight *inflight;
268 struct vhost_scsi_virtqueue *svq;
269
270 svq = container_of(vq, struct vhost_scsi_virtqueue, vq);
271 inflight = &svq->inflights[svq->inflight_idx];
272 kref_get(&inflight->kref);
273
274 return inflight;
275}
276
277static void tcm_vhost_put_inflight(struct vhost_scsi_inflight *inflight)
278{
279 kref_put(&inflight->kref, tcm_vhost_done_inflight);
280}
281
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700282static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
283{
284 return 1;
285}
286
287static int tcm_vhost_check_false(struct se_portal_group *se_tpg)
288{
289 return 0;
290}
291
292static char *tcm_vhost_get_fabric_name(void)
293{
294 return "vhost";
295}
296
297static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg)
298{
299 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
300 struct tcm_vhost_tpg, se_tpg);
301 struct tcm_vhost_tport *tport = tpg->tport;
302
303 switch (tport->tport_proto_id) {
304 case SCSI_PROTOCOL_SAS:
305 return sas_get_fabric_proto_ident(se_tpg);
306 case SCSI_PROTOCOL_FCP:
307 return fc_get_fabric_proto_ident(se_tpg);
308 case SCSI_PROTOCOL_ISCSI:
309 return iscsi_get_fabric_proto_ident(se_tpg);
310 default:
311 pr_err("Unknown tport_proto_id: 0x%02x, using"
312 " SAS emulation\n", tport->tport_proto_id);
313 break;
314 }
315
316 return sas_get_fabric_proto_ident(se_tpg);
317}
318
319static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg)
320{
321 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
322 struct tcm_vhost_tpg, se_tpg);
323 struct tcm_vhost_tport *tport = tpg->tport;
324
325 return &tport->tport_name[0];
326}
327
328static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg)
329{
330 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
331 struct tcm_vhost_tpg, se_tpg);
332 return tpg->tport_tpgt;
333}
334
335static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
336{
337 return 1;
338}
339
Asias He683bd962013-05-06 16:38:27 +0800340static u32
341tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
342 struct se_node_acl *se_nacl,
343 struct t10_pr_registration *pr_reg,
344 int *format_code,
345 unsigned char *buf)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700346{
347 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
348 struct tcm_vhost_tpg, se_tpg);
349 struct tcm_vhost_tport *tport = tpg->tport;
350
351 switch (tport->tport_proto_id) {
352 case SCSI_PROTOCOL_SAS:
353 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
354 format_code, buf);
355 case SCSI_PROTOCOL_FCP:
356 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
357 format_code, buf);
358 case SCSI_PROTOCOL_ISCSI:
359 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
360 format_code, buf);
361 default:
362 pr_err("Unknown tport_proto_id: 0x%02x, using"
363 " SAS emulation\n", tport->tport_proto_id);
364 break;
365 }
366
367 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
368 format_code, buf);
369}
370
Asias He683bd962013-05-06 16:38:27 +0800371static u32
372tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
373 struct se_node_acl *se_nacl,
374 struct t10_pr_registration *pr_reg,
375 int *format_code)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700376{
377 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
378 struct tcm_vhost_tpg, se_tpg);
379 struct tcm_vhost_tport *tport = tpg->tport;
380
381 switch (tport->tport_proto_id) {
382 case SCSI_PROTOCOL_SAS:
383 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
384 format_code);
385 case SCSI_PROTOCOL_FCP:
386 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
387 format_code);
388 case SCSI_PROTOCOL_ISCSI:
389 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
390 format_code);
391 default:
392 pr_err("Unknown tport_proto_id: 0x%02x, using"
393 " SAS emulation\n", tport->tport_proto_id);
394 break;
395 }
396
397 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
398 format_code);
399}
400
Asias He683bd962013-05-06 16:38:27 +0800401static char *
402tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
403 const char *buf,
404 u32 *out_tid_len,
405 char **port_nexus_ptr)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700406{
407 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
408 struct tcm_vhost_tpg, se_tpg);
409 struct tcm_vhost_tport *tport = tpg->tport;
410
411 switch (tport->tport_proto_id) {
412 case SCSI_PROTOCOL_SAS:
413 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
414 port_nexus_ptr);
415 case SCSI_PROTOCOL_FCP:
416 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
417 port_nexus_ptr);
418 case SCSI_PROTOCOL_ISCSI:
419 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
420 port_nexus_ptr);
421 default:
422 pr_err("Unknown tport_proto_id: 0x%02x, using"
423 " SAS emulation\n", tport->tport_proto_id);
424 break;
425 }
426
427 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
428 port_nexus_ptr);
429}
430
Asias He683bd962013-05-06 16:38:27 +0800431static struct se_node_acl *
432tcm_vhost_alloc_fabric_acl(struct se_portal_group *se_tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700433{
434 struct tcm_vhost_nacl *nacl;
435
436 nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL);
437 if (!nacl) {
Masanari Iida744627e92012-11-05 23:30:40 +0900438 pr_err("Unable to allocate struct tcm_vhost_nacl\n");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700439 return NULL;
440 }
441
442 return &nacl->se_node_acl;
443}
444
Asias He683bd962013-05-06 16:38:27 +0800445static void
446tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
447 struct se_node_acl *se_nacl)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700448{
449 struct tcm_vhost_nacl *nacl = container_of(se_nacl,
450 struct tcm_vhost_nacl, se_node_acl);
451 kfree(nacl);
452}
453
454static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg)
455{
456 return 1;
457}
458
459static void tcm_vhost_release_cmd(struct se_cmd *se_cmd)
460{
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700461 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
462 struct tcm_vhost_cmd, tvc_se_cmd);
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700463 struct se_session *se_sess = se_cmd->se_sess;
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800464 int i;
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700465
466 if (tv_cmd->tvc_sgl_count) {
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700467 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
468 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
Michael S. Tsirkind3d665a2013-09-17 22:54:31 +0300469 }
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800470 if (tv_cmd->tvc_prot_sgl_count) {
471 for (i = 0; i < tv_cmd->tvc_prot_sgl_count; i++)
472 put_page(sg_page(&tv_cmd->tvc_prot_sgl[i]));
473 }
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700474
475 tcm_vhost_put_inflight(tv_cmd->inflight);
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700476 percpu_ida_free(&se_sess->sess_tag_pool, se_cmd->map_tag);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700477}
478
479static int tcm_vhost_shutdown_session(struct se_session *se_sess)
480{
481 return 0;
482}
483
484static void tcm_vhost_close_session(struct se_session *se_sess)
485{
486 return;
487}
488
489static u32 tcm_vhost_sess_get_index(struct se_session *se_sess)
490{
491 return 0;
492}
493
494static int tcm_vhost_write_pending(struct se_cmd *se_cmd)
495{
496 /* Go ahead and process the write immediately */
497 target_execute_cmd(se_cmd);
498 return 0;
499}
500
501static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd)
502{
503 return 0;
504}
505
506static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl)
507{
508 return;
509}
510
511static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd)
512{
513 return 0;
514}
515
516static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
517{
518 return 0;
519}
520
Asias He3c63f662013-05-06 16:38:29 +0800521static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *cmd)
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700522{
Asias He3c63f662013-05-06 16:38:29 +0800523 struct vhost_scsi *vs = cmd->tvc_vhost;
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700524
Asias He3c63f662013-05-06 16:38:29 +0800525 llist_add(&cmd->tvc_completion_list, &vs->vs_completion_list);
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700526
527 vhost_work_queue(&vs->dev, &vs->vs_completion_work);
528}
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700529
530static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
531{
Asias He3c63f662013-05-06 16:38:29 +0800532 struct tcm_vhost_cmd *cmd = container_of(se_cmd,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700533 struct tcm_vhost_cmd, tvc_se_cmd);
Asias He3c63f662013-05-06 16:38:29 +0800534 vhost_scsi_complete_cmd(cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700535 return 0;
536}
537
538static int tcm_vhost_queue_status(struct se_cmd *se_cmd)
539{
Asias He3c63f662013-05-06 16:38:29 +0800540 struct tcm_vhost_cmd *cmd = container_of(se_cmd,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700541 struct tcm_vhost_cmd, tvc_se_cmd);
Asias He3c63f662013-05-06 16:38:29 +0800542 vhost_scsi_complete_cmd(cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700543 return 0;
544}
545
Joern Engelb79fafa2013-07-03 11:22:17 -0400546static void tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700547{
Joern Engelb79fafa2013-07-03 11:22:17 -0400548 return;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700549}
550
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700551static void tcm_vhost_aborted_task(struct se_cmd *se_cmd)
552{
553 return;
554}
555
Asias Hea6c9af82013-04-25 15:35:21 +0800556static void tcm_vhost_free_evt(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
557{
558 vs->vs_events_nr--;
559 kfree(evt);
560}
561
Asias He683bd962013-05-06 16:38:27 +0800562static struct tcm_vhost_evt *
563tcm_vhost_allocate_evt(struct vhost_scsi *vs,
564 u32 event, u32 reason)
Asias Hea6c9af82013-04-25 15:35:21 +0800565{
Asias He3ab2e422013-04-27 11:16:48 +0800566 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Asias Hea6c9af82013-04-25 15:35:21 +0800567 struct tcm_vhost_evt *evt;
568
569 if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) {
570 vs->vs_events_missed = true;
571 return NULL;
572 }
573
574 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
575 if (!evt) {
576 vq_err(vq, "Failed to allocate tcm_vhost_evt\n");
577 vs->vs_events_missed = true;
578 return NULL;
579 }
580
Michael S. Tsirkine72fd722014-11-23 18:01:34 +0200581 evt->event.event = cpu_to_vhost32(vq, event);
582 evt->event.reason = cpu_to_vhost32(vq, reason);
Asias Hea6c9af82013-04-25 15:35:21 +0800583 vs->vs_events_nr++;
584
585 return evt;
586}
587
Asias He3c63f662013-05-06 16:38:29 +0800588static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700589{
Asias He3c63f662013-05-06 16:38:29 +0800590 struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700591
592 /* TODO locking against target/backend threads? */
Nicholas Bellinger6c131d02013-06-06 01:44:48 -0700593 transport_generic_free_cmd(se_cmd, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700594
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700595}
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700596
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700597static int vhost_scsi_check_stop_free(struct se_cmd *se_cmd)
598{
599 return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700600}
601
Asias He683bd962013-05-06 16:38:27 +0800602static void
603tcm_vhost_do_evt_work(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
Asias Hea6c9af82013-04-25 15:35:21 +0800604{
Asias He3ab2e422013-04-27 11:16:48 +0800605 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Asias Hea6c9af82013-04-25 15:35:21 +0800606 struct virtio_scsi_event *event = &evt->event;
607 struct virtio_scsi_event __user *eventp;
608 unsigned out, in;
609 int head, ret;
610
611 if (!vq->private_data) {
612 vs->vs_events_missed = true;
613 return;
614 }
615
616again:
617 vhost_disable_notify(&vs->dev, vq);
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300618 head = vhost_get_vq_desc(vq, vq->iov,
Asias Hea6c9af82013-04-25 15:35:21 +0800619 ARRAY_SIZE(vq->iov), &out, &in,
620 NULL, NULL);
621 if (head < 0) {
622 vs->vs_events_missed = true;
623 return;
624 }
625 if (head == vq->num) {
626 if (vhost_enable_notify(&vs->dev, vq))
627 goto again;
628 vs->vs_events_missed = true;
629 return;
630 }
631
632 if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) {
633 vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n",
634 vq->iov[out].iov_len);
635 vs->vs_events_missed = true;
636 return;
637 }
638
639 if (vs->vs_events_missed) {
Michael S. Tsirkine72fd722014-11-23 18:01:34 +0200640 event->event |= cpu_to_vhost32(vq, VIRTIO_SCSI_T_EVENTS_MISSED);
Asias Hea6c9af82013-04-25 15:35:21 +0800641 vs->vs_events_missed = false;
642 }
643
644 eventp = vq->iov[out].iov_base;
645 ret = __copy_to_user(eventp, event, sizeof(*event));
646 if (!ret)
647 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
648 else
649 vq_err(vq, "Faulted on tcm_vhost_send_event\n");
650}
651
652static void tcm_vhost_evt_work(struct vhost_work *work)
653{
654 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
655 vs_event_work);
Asias He3ab2e422013-04-27 11:16:48 +0800656 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Asias Hea6c9af82013-04-25 15:35:21 +0800657 struct tcm_vhost_evt *evt;
658 struct llist_node *llnode;
659
660 mutex_lock(&vq->mutex);
661 llnode = llist_del_all(&vs->vs_event_list);
662 while (llnode) {
663 evt = llist_entry(llnode, struct tcm_vhost_evt, list);
664 llnode = llist_next(llnode);
665 tcm_vhost_do_evt_work(vs, evt);
666 tcm_vhost_free_evt(vs, evt);
667 }
668 mutex_unlock(&vq->mutex);
669}
670
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700671/* Fill in status and signal that we are done processing this command
672 *
673 * This is scheduled in the vhost work queue so we are called with the owner
674 * process mm and can access the vring.
675 */
676static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
677{
678 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
679 vs_completion_work);
Asias He1b7f3902013-02-06 13:20:59 +0800680 DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
Asias He9d6064a2013-01-06 14:36:13 +0800681 struct virtio_scsi_cmd_resp v_rsp;
Asias He3c63f662013-05-06 16:38:29 +0800682 struct tcm_vhost_cmd *cmd;
Asias He9d6064a2013-01-06 14:36:13 +0800683 struct llist_node *llnode;
684 struct se_cmd *se_cmd;
Asias He1b7f3902013-02-06 13:20:59 +0800685 int ret, vq;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700686
Asias He1b7f3902013-02-06 13:20:59 +0800687 bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
Asias He9d6064a2013-01-06 14:36:13 +0800688 llnode = llist_del_all(&vs->vs_completion_list);
689 while (llnode) {
Asias He3c63f662013-05-06 16:38:29 +0800690 cmd = llist_entry(llnode, struct tcm_vhost_cmd,
Asias He9d6064a2013-01-06 14:36:13 +0800691 tvc_completion_list);
692 llnode = llist_next(llnode);
Asias He3c63f662013-05-06 16:38:29 +0800693 se_cmd = &cmd->tvc_se_cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700694
695 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
Asias He3c63f662013-05-06 16:38:29 +0800696 cmd, se_cmd->residual_count, se_cmd->scsi_status);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700697
698 memset(&v_rsp, 0, sizeof(v_rsp));
Michael S. Tsirkine72fd722014-11-23 18:01:34 +0200699 v_rsp.resid = cpu_to_vhost32(cmd->tvc_vq, se_cmd->residual_count);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700700 /* TODO is status_qualifier field needed? */
701 v_rsp.status = se_cmd->scsi_status;
Michael S. Tsirkine72fd722014-11-23 18:01:34 +0200702 v_rsp.sense_len = cpu_to_vhost32(cmd->tvc_vq,
703 se_cmd->scsi_sense_length);
Asias He3c63f662013-05-06 16:38:29 +0800704 memcpy(v_rsp.sense, cmd->tvc_sense_buf,
Michael S. Tsirkine72fd722014-11-23 18:01:34 +0200705 se_cmd->scsi_sense_length);
Asias He3c63f662013-05-06 16:38:29 +0800706 ret = copy_to_user(cmd->tvc_resp, &v_rsp, sizeof(v_rsp));
Asias He1b7f3902013-02-06 13:20:59 +0800707 if (likely(ret == 0)) {
Asias He3ab2e422013-04-27 11:16:48 +0800708 struct vhost_scsi_virtqueue *q;
Asias He3c63f662013-05-06 16:38:29 +0800709 vhost_add_used(cmd->tvc_vq, cmd->tvc_vq_desc, 0);
710 q = container_of(cmd->tvc_vq, struct vhost_scsi_virtqueue, vq);
Asias He3ab2e422013-04-27 11:16:48 +0800711 vq = q - vs->vqs;
Asias He1b7f3902013-02-06 13:20:59 +0800712 __set_bit(vq, signal);
713 } else
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700714 pr_err("Faulted on virtio_scsi_cmd_resp\n");
715
Asias He3c63f662013-05-06 16:38:29 +0800716 vhost_scsi_free_cmd(cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700717 }
718
Asias He1b7f3902013-02-06 13:20:59 +0800719 vq = -1;
720 while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1))
721 < VHOST_SCSI_MAX_VQ)
Asias He3ab2e422013-04-27 11:16:48 +0800722 vhost_signal(&vs->dev, &vs->vqs[vq].vq);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700723}
724
Asias He683bd962013-05-06 16:38:27 +0800725static struct tcm_vhost_cmd *
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800726vhost_scsi_get_tag(struct vhost_virtqueue *vq, struct tcm_vhost_tpg *tpg,
727 unsigned char *cdb, u64 scsi_tag, u16 lun, u8 task_attr,
728 u32 exp_data_len, int data_direction)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700729{
Asias He3c63f662013-05-06 16:38:29 +0800730 struct tcm_vhost_cmd *cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700731 struct tcm_vhost_nexus *tv_nexus;
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700732 struct se_session *se_sess;
Nicholas Bellingerb1935f62014-02-22 18:08:24 -0800733 struct scatterlist *sg, *prot_sg;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700734 struct page **pages;
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700735 int tag;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700736
Asias He98718312013-05-06 16:38:28 +0800737 tv_nexus = tpg->tpg_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700738 if (!tv_nexus) {
739 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
740 return ERR_PTR(-EIO);
741 }
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700742 se_sess = tv_nexus->tvn_se_sess;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700743
Kent Overstreet6f6b5d12014-01-19 08:26:37 +0000744 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
Nicholas Bellinger4a47d3a2013-09-23 11:42:28 -0700745 if (tag < 0) {
746 pr_err("Unable to obtain tag for tcm_vhost_cmd\n");
747 return ERR_PTR(-ENOMEM);
748 }
749
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700750 cmd = &((struct tcm_vhost_cmd *)se_sess->sess_cmd_map)[tag];
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700751 sg = cmd->tvc_sgl;
Nicholas Bellingerb1935f62014-02-22 18:08:24 -0800752 prot_sg = cmd->tvc_prot_sgl;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700753 pages = cmd->tvc_upages;
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700754 memset(cmd, 0, sizeof(struct tcm_vhost_cmd));
755
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700756 cmd->tvc_sgl = sg;
Nicholas Bellingerb1935f62014-02-22 18:08:24 -0800757 cmd->tvc_prot_sgl = prot_sg;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700758 cmd->tvc_upages = pages;
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700759 cmd->tvc_se_cmd.map_tag = tag;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800760 cmd->tvc_tag = scsi_tag;
761 cmd->tvc_lun = lun;
762 cmd->tvc_task_attr = task_attr;
Asias He3c63f662013-05-06 16:38:29 +0800763 cmd->tvc_exp_data_len = exp_data_len;
764 cmd->tvc_data_direction = data_direction;
765 cmd->tvc_nexus = tv_nexus;
766 cmd->inflight = tcm_vhost_get_inflight(vq);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700767
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800768 memcpy(cmd->tvc_cdb, cdb, TCM_VHOST_MAX_CDB_SIZE);
769
Asias He3c63f662013-05-06 16:38:29 +0800770 return cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700771}
772
773/*
774 * Map a user memory range into a scatterlist
775 *
776 * Returns the number of scatterlist entries used or -errno on error.
777 */
Asias He683bd962013-05-06 16:38:27 +0800778static int
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700779vhost_scsi_map_to_sgl(struct tcm_vhost_cmd *tv_cmd,
780 struct scatterlist *sgl,
Asias He683bd962013-05-06 16:38:27 +0800781 unsigned int sgl_count,
782 struct iovec *iov,
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800783 struct page **pages,
784 bool write)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700785{
Asias He18100532013-01-22 11:20:27 +0800786 unsigned int npages = 0, pages_nr, offset, nbytes;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700787 struct scatterlist *sg = sgl;
Asias He18100532013-01-22 11:20:27 +0800788 void __user *ptr = iov->iov_base;
789 size_t len = iov->iov_len;
Asias He18100532013-01-22 11:20:27 +0800790 int ret, i;
791
792 pages_nr = iov_num_pages(iov);
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800793 if (pages_nr > sgl_count) {
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700794 pr_err("vhost_scsi_map_to_sgl() pages_nr: %u greater than"
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800795 " sgl_count: %u\n", pages_nr, sgl_count);
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700796 return -ENOBUFS;
797 }
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800798 if (pages_nr > TCM_VHOST_PREALLOC_UPAGES) {
799 pr_err("vhost_scsi_map_to_sgl() pages_nr: %u greater than"
800 " preallocated TCM_VHOST_PREALLOC_UPAGES: %u\n",
801 pages_nr, TCM_VHOST_PREALLOC_UPAGES);
802 return -ENOBUFS;
803 }
Asias He18100532013-01-22 11:20:27 +0800804
805 ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages);
806 /* No pages were pinned */
807 if (ret < 0)
808 goto out;
809 /* Less pages pinned than wanted */
810 if (ret != pages_nr) {
811 for (i = 0; i < ret; i++)
812 put_page(pages[i]);
813 ret = -EFAULT;
814 goto out;
815 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700816
817 while (len > 0) {
Asias He18100532013-01-22 11:20:27 +0800818 offset = (uintptr_t)ptr & ~PAGE_MASK;
819 nbytes = min_t(unsigned int, PAGE_SIZE - offset, len);
820 sg_set_page(sg, pages[npages], nbytes, offset);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700821 ptr += nbytes;
822 len -= nbytes;
823 sg++;
824 npages++;
825 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700826
Asias He18100532013-01-22 11:20:27 +0800827out:
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700828 return ret;
829}
830
Asias He683bd962013-05-06 16:38:27 +0800831static int
Asias He3c63f662013-05-06 16:38:29 +0800832vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *cmd,
Asias He683bd962013-05-06 16:38:27 +0800833 struct iovec *iov,
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800834 int niov,
835 bool write)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700836{
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800837 struct scatterlist *sg = cmd->tvc_sgl;
838 unsigned int sgl_count = 0;
839 int ret, i;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700840
Asias Hef3158f32013-01-22 11:20:26 +0800841 for (i = 0; i < niov; i++)
842 sgl_count += iov_num_pages(&iov[i]);
843
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800844 if (sgl_count > TCM_VHOST_PREALLOC_SGLS) {
845 pr_err("vhost_scsi_map_iov_to_sgl() sgl_count: %u greater than"
846 " preallocated TCM_VHOST_PREALLOC_SGLS: %u\n",
847 sgl_count, TCM_VHOST_PREALLOC_SGLS);
848 return -ENOBUFS;
849 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700850
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700851 pr_debug("%s sg %p sgl_count %u\n", __func__, sg, sgl_count);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700852 sg_init_table(sg, sgl_count);
Asias He3c63f662013-05-06 16:38:29 +0800853 cmd->tvc_sgl_count = sgl_count;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700854
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800855 pr_debug("Mapping iovec %p for %u pages\n", &iov[0], sgl_count);
856
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700857 for (i = 0; i < niov; i++) {
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700858 ret = vhost_scsi_map_to_sgl(cmd, sg, sgl_count, &iov[i],
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800859 cmd->tvc_upages, write);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700860 if (ret < 0) {
Asias He3c63f662013-05-06 16:38:29 +0800861 for (i = 0; i < cmd->tvc_sgl_count; i++)
862 put_page(sg_page(&cmd->tvc_sgl[i]));
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700863
Asias He3c63f662013-05-06 16:38:29 +0800864 cmd->tvc_sgl_count = 0;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700865 return ret;
866 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700867 sg += ret;
868 sgl_count -= ret;
869 }
870 return 0;
871}
872
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800873static int
874vhost_scsi_map_iov_to_prot(struct tcm_vhost_cmd *cmd,
875 struct iovec *iov,
876 int niov,
877 bool write)
878{
879 struct scatterlist *prot_sg = cmd->tvc_prot_sgl;
880 unsigned int prot_sgl_count = 0;
881 int ret, i;
882
883 for (i = 0; i < niov; i++)
884 prot_sgl_count += iov_num_pages(&iov[i]);
885
886 if (prot_sgl_count > TCM_VHOST_PREALLOC_PROT_SGLS) {
887 pr_err("vhost_scsi_map_iov_to_prot() sgl_count: %u greater than"
888 " preallocated TCM_VHOST_PREALLOC_PROT_SGLS: %u\n",
889 prot_sgl_count, TCM_VHOST_PREALLOC_PROT_SGLS);
890 return -ENOBUFS;
891 }
892
893 pr_debug("%s prot_sg %p prot_sgl_count %u\n", __func__,
894 prot_sg, prot_sgl_count);
895 sg_init_table(prot_sg, prot_sgl_count);
896 cmd->tvc_prot_sgl_count = prot_sgl_count;
897
898 for (i = 0; i < niov; i++) {
899 ret = vhost_scsi_map_to_sgl(cmd, prot_sg, prot_sgl_count, &iov[i],
900 cmd->tvc_upages, write);
901 if (ret < 0) {
902 for (i = 0; i < cmd->tvc_prot_sgl_count; i++)
903 put_page(sg_page(&cmd->tvc_prot_sgl[i]));
904
905 cmd->tvc_prot_sgl_count = 0;
906 return ret;
907 }
908 prot_sg += ret;
909 prot_sgl_count -= ret;
910 }
911 return 0;
912}
913
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700914static void tcm_vhost_submission_work(struct work_struct *work)
915{
Asias He3c63f662013-05-06 16:38:29 +0800916 struct tcm_vhost_cmd *cmd =
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700917 container_of(work, struct tcm_vhost_cmd, work);
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700918 struct tcm_vhost_nexus *tv_nexus;
Asias He3c63f662013-05-06 16:38:29 +0800919 struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800920 struct scatterlist *sg_ptr, *sg_prot_ptr = NULL;
921 int rc;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700922
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800923 /* FIXME: BIDI operation */
Asias He3c63f662013-05-06 16:38:29 +0800924 if (cmd->tvc_sgl_count) {
925 sg_ptr = cmd->tvc_sgl;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800926
927 if (cmd->tvc_prot_sgl_count)
928 sg_prot_ptr = cmd->tvc_prot_sgl;
929 else
930 se_cmd->prot_pto = true;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700931 } else {
932 sg_ptr = NULL;
933 }
Asias He3c63f662013-05-06 16:38:29 +0800934 tv_nexus = cmd->tvc_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700935
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700936 rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
Asias He3c63f662013-05-06 16:38:29 +0800937 cmd->tvc_cdb, &cmd->tvc_sense_buf[0],
938 cmd->tvc_lun, cmd->tvc_exp_data_len,
939 cmd->tvc_task_attr, cmd->tvc_data_direction,
Linus Torvalds6d2fa9e2013-07-11 12:57:19 -0700940 TARGET_SCF_ACK_KREF, sg_ptr, cmd->tvc_sgl_count,
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800941 NULL, 0, sg_prot_ptr, cmd->tvc_prot_sgl_count);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700942 if (rc < 0) {
943 transport_send_check_condition_and_sense(se_cmd,
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700944 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700945 transport_generic_free_cmd(se_cmd, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700946 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700947}
948
Asias He683bd962013-05-06 16:38:27 +0800949static void
950vhost_scsi_send_bad_target(struct vhost_scsi *vs,
951 struct vhost_virtqueue *vq,
952 int head, unsigned out)
Asias He637ab212013-04-10 15:06:15 +0800953{
954 struct virtio_scsi_cmd_resp __user *resp;
955 struct virtio_scsi_cmd_resp rsp;
956 int ret;
957
958 memset(&rsp, 0, sizeof(rsp));
959 rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
960 resp = vq->iov[out].iov_base;
961 ret = __copy_to_user(resp, &rsp, sizeof(rsp));
962 if (!ret)
963 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
964 else
965 pr_err("Faulted on virtio_scsi_cmd_resp\n");
966}
967
Asias He683bd962013-05-06 16:38:27 +0800968static void
969vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700970{
Asias He4f7f46d2013-04-03 14:17:37 +0800971 struct tcm_vhost_tpg **vs_tpg;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700972 struct virtio_scsi_cmd_req v_req;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800973 struct virtio_scsi_cmd_req_pi v_req_pi;
Asias He98718312013-05-06 16:38:28 +0800974 struct tcm_vhost_tpg *tpg;
Asias He3c63f662013-05-06 16:38:29 +0800975 struct tcm_vhost_cmd *cmd;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800976 u64 tag;
977 u32 exp_data_len, data_first, data_num, data_direction, prot_first;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700978 unsigned out, in, i;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800979 int head, ret, data_niov, prot_niov, prot_bytes;
980 size_t req_size;
981 u16 lun;
982 u8 *target, *lunp, task_attr;
983 bool hdr_pi;
984 void *req, *cdb;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700985
Asias Hee7802212013-05-07 14:54:35 +0800986 mutex_lock(&vq->mutex);
Asias He4f7f46d2013-04-03 14:17:37 +0800987 /*
988 * We can handle the vq only after the endpoint is setup by calling the
989 * VHOST_SCSI_SET_ENDPOINT ioctl.
Asias He4f7f46d2013-04-03 14:17:37 +0800990 */
Asias Hee7802212013-05-07 14:54:35 +0800991 vs_tpg = vq->private_data;
Asias He4f7f46d2013-04-03 14:17:37 +0800992 if (!vs_tpg)
Asias Hee7802212013-05-07 14:54:35 +0800993 goto out;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700994
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700995 vhost_disable_notify(&vs->dev, vq);
996
997 for (;;) {
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300998 head = vhost_get_vq_desc(vq, vq->iov,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700999 ARRAY_SIZE(vq->iov), &out, &in,
1000 NULL, NULL);
1001 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
1002 head, out, in);
1003 /* On error, stop handling until the next kick. */
1004 if (unlikely(head < 0))
1005 break;
1006 /* Nothing new? Wait for eventfd to tell us they refilled. */
1007 if (head == vq->num) {
1008 if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
1009 vhost_disable_notify(&vs->dev, vq);
1010 continue;
1011 }
1012 break;
1013 }
1014
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001015 /* FIXME: BIDI operation */
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001016 if (out == 1 && in == 1) {
1017 data_direction = DMA_NONE;
1018 data_first = 0;
1019 data_num = 0;
1020 } else if (out == 1 && in > 1) {
1021 data_direction = DMA_FROM_DEVICE;
1022 data_first = out + 1;
1023 data_num = in - 1;
1024 } else if (out > 1 && in == 1) {
1025 data_direction = DMA_TO_DEVICE;
1026 data_first = 1;
1027 data_num = out - 1;
1028 } else {
1029 vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
1030 out, in);
1031 break;
1032 }
1033
1034 /*
1035 * Check for a sane resp buffer so we can report errors to
1036 * the guest.
1037 */
1038 if (unlikely(vq->iov[out].iov_len !=
1039 sizeof(struct virtio_scsi_cmd_resp))) {
1040 vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
1041 " bytes\n", vq->iov[out].iov_len);
1042 break;
1043 }
1044
Linus Torvaldsed9ea4e2014-06-12 22:38:32 -07001045 if (vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI)) {
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001046 req = &v_req_pi;
1047 lunp = &v_req_pi.lun[0];
1048 target = &v_req_pi.lun[1];
1049 req_size = sizeof(v_req_pi);
1050 hdr_pi = true;
1051 } else {
1052 req = &v_req;
1053 lunp = &v_req.lun[0];
1054 target = &v_req.lun[1];
1055 req_size = sizeof(v_req);
1056 hdr_pi = false;
1057 }
1058
1059 if (unlikely(vq->iov[0].iov_len < req_size)) {
1060 pr_err("Expecting virtio-scsi header: %zu, got %zu\n",
1061 req_size, vq->iov[0].iov_len);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001062 break;
1063 }
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001064 ret = memcpy_fromiovecend(req, &vq->iov[0], 0, req_size);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001065 if (unlikely(ret)) {
1066 vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
1067 break;
1068 }
1069
Venkatesh Srinivas7fe412d2014-02-24 14:13:32 -08001070 /* virtio-scsi spec requires byte 0 of the lun to be 1 */
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001071 if (unlikely(*lunp != 1)) {
Venkatesh Srinivas7fe412d2014-02-24 14:13:32 -08001072 vhost_scsi_send_bad_target(vs, vq, head, out);
1073 continue;
1074 }
1075
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001076 tpg = ACCESS_ONCE(vs_tpg[*target]);
Asias He67e18cf2013-02-05 12:31:57 +08001077
1078 /* Target does not exist, fail the request */
Asias He98718312013-05-06 16:38:28 +08001079 if (unlikely(!tpg)) {
Asias He637ab212013-04-10 15:06:15 +08001080 vhost_scsi_send_bad_target(vs, vq, head, out);
Asias He67e18cf2013-02-05 12:31:57 +08001081 continue;
1082 }
1083
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001084 data_niov = data_num;
1085 prot_niov = prot_first = prot_bytes = 0;
1086 /*
1087 * Determine if any protection information iovecs are preceeding
1088 * the actual data payload, and adjust data_first + data_niov
1089 * values accordingly for vhost_scsi_map_iov_to_sgl() below.
1090 *
1091 * Also extract virtio_scsi header bits for vhost_scsi_get_tag()
1092 */
1093 if (hdr_pi) {
1094 if (v_req_pi.pi_bytesout) {
1095 if (data_direction != DMA_TO_DEVICE) {
1096 vq_err(vq, "Received non zero do_pi_niov"
1097 ", but wrong data_direction\n");
1098 goto err_cmd;
1099 }
Michael S. Tsirkine72fd722014-11-23 18:01:34 +02001100 prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesout);
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001101 } else if (v_req_pi.pi_bytesin) {
1102 if (data_direction != DMA_FROM_DEVICE) {
1103 vq_err(vq, "Received non zero di_pi_niov"
1104 ", but wrong data_direction\n");
1105 goto err_cmd;
1106 }
Michael S. Tsirkine72fd722014-11-23 18:01:34 +02001107 prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesin);
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001108 }
1109 if (prot_bytes) {
1110 int tmp = 0;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001111
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001112 for (i = 0; i < data_num; i++) {
1113 tmp += vq->iov[data_first + i].iov_len;
1114 prot_niov++;
1115 if (tmp >= prot_bytes)
1116 break;
1117 }
1118 prot_first = data_first;
1119 data_first += prot_niov;
1120 data_niov = data_num - prot_niov;
1121 }
Michael S. Tsirkine72fd722014-11-23 18:01:34 +02001122 tag = vhost64_to_cpu(vq, v_req_pi.tag);
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001123 task_attr = v_req_pi.task_attr;
1124 cdb = &v_req_pi.cdb[0];
1125 lun = ((v_req_pi.lun[2] << 8) | v_req_pi.lun[3]) & 0x3FFF;
1126 } else {
Michael S. Tsirkine72fd722014-11-23 18:01:34 +02001127 tag = vhost64_to_cpu(vq, v_req.tag);
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001128 task_attr = v_req.task_attr;
1129 cdb = &v_req.cdb[0];
1130 lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
1131 }
1132 exp_data_len = 0;
1133 for (i = 0; i < data_niov; i++)
1134 exp_data_len += vq->iov[data_first + i].iov_len;
1135 /*
1136 * Check that the recieved CDB size does not exceeded our
1137 * hardcoded max for vhost-scsi
1138 *
1139 * TODO what if cdb was too small for varlen cdb header?
1140 */
1141 if (unlikely(scsi_command_size(cdb) > TCM_VHOST_MAX_CDB_SIZE)) {
1142 vq_err(vq, "Received SCSI CDB with command_size: %d that"
1143 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1144 scsi_command_size(cdb), TCM_VHOST_MAX_CDB_SIZE);
1145 goto err_cmd;
1146 }
1147
1148 cmd = vhost_scsi_get_tag(vq, tpg, cdb, tag, lun, task_attr,
Nicholas Bellinger9f977ef2014-06-10 01:19:38 -07001149 exp_data_len + prot_bytes,
1150 data_direction);
Asias He3c63f662013-05-06 16:38:29 +08001151 if (IS_ERR(cmd)) {
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -07001152 vq_err(vq, "vhost_scsi_get_tag failed %ld\n",
Asias He3c63f662013-05-06 16:38:29 +08001153 PTR_ERR(cmd));
Asias He055f6482013-04-10 15:06:16 +08001154 goto err_cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001155 }
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001156
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001157 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
Asias He3c63f662013-05-06 16:38:29 +08001158 ": %d\n", cmd, exp_data_len, data_direction);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001159
Asias He3c63f662013-05-06 16:38:29 +08001160 cmd->tvc_vhost = vs;
1161 cmd->tvc_vq = vq;
1162 cmd->tvc_resp = vq->iov[out].iov_base;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001163
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001164 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
Asias He3c63f662013-05-06 16:38:29 +08001165 cmd->tvc_cdb[0], cmd->tvc_lun);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001166
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001167 if (prot_niov) {
1168 ret = vhost_scsi_map_iov_to_prot(cmd,
1169 &vq->iov[prot_first], prot_niov,
1170 data_direction == DMA_FROM_DEVICE);
1171 if (unlikely(ret)) {
1172 vq_err(vq, "Failed to map iov to"
1173 " prot_sgl\n");
1174 goto err_free;
1175 }
1176 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001177 if (data_direction != DMA_NONE) {
Asias He3c63f662013-05-06 16:38:29 +08001178 ret = vhost_scsi_map_iov_to_sgl(cmd,
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001179 &vq->iov[data_first], data_niov,
Nicholas Bellinger60a01f52013-10-25 10:44:15 -07001180 data_direction == DMA_FROM_DEVICE);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001181 if (unlikely(ret)) {
1182 vq_err(vq, "Failed to map iov to sgl\n");
Asias He055f6482013-04-10 15:06:16 +08001183 goto err_free;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001184 }
1185 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001186 /*
1187 * Save the descriptor from vhost_get_vq_desc() to be used to
1188 * complete the virtio-scsi request in TCM callback context via
1189 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
1190 */
Asias He3c63f662013-05-06 16:38:29 +08001191 cmd->tvc_vq_desc = head;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001192 /*
1193 * Dispatch tv_cmd descriptor for cmwq execution in process
1194 * context provided by tcm_vhost_workqueue. This also ensures
1195 * tv_cmd is executed on the same kworker CPU as this vhost
1196 * thread to gain positive L2 cache locality effects..
1197 */
Asias He3c63f662013-05-06 16:38:29 +08001198 INIT_WORK(&cmd->work, tcm_vhost_submission_work);
1199 queue_work(tcm_vhost_workqueue, &cmd->work);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001200 }
1201
1202 mutex_unlock(&vq->mutex);
Asias He7ea206c2013-04-10 15:06:14 +08001203 return;
1204
Asias He055f6482013-04-10 15:06:16 +08001205err_free:
Asias He3c63f662013-05-06 16:38:29 +08001206 vhost_scsi_free_cmd(cmd);
Asias He055f6482013-04-10 15:06:16 +08001207err_cmd:
1208 vhost_scsi_send_bad_target(vs, vq, head, out);
Asias Hee7802212013-05-07 14:54:35 +08001209out:
Asias He7ea206c2013-04-10 15:06:14 +08001210 mutex_unlock(&vq->mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001211}
1212
1213static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
1214{
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001215 pr_debug("%s: The handling func for control queue.\n", __func__);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001216}
1217
Asias He683bd962013-05-06 16:38:27 +08001218static void
1219tcm_vhost_send_evt(struct vhost_scsi *vs,
1220 struct tcm_vhost_tpg *tpg,
1221 struct se_lun *lun,
1222 u32 event,
1223 u32 reason)
Asias Hea6c9af82013-04-25 15:35:21 +08001224{
1225 struct tcm_vhost_evt *evt;
1226
1227 evt = tcm_vhost_allocate_evt(vs, event, reason);
1228 if (!evt)
1229 return;
1230
1231 if (tpg && lun) {
1232 /* TODO: share lun setup code with virtio-scsi.ko */
1233 /*
1234 * Note: evt->event is zeroed when we allocate it and
1235 * lun[4-7] need to be zero according to virtio-scsi spec.
1236 */
1237 evt->event.lun[0] = 0x01;
1238 evt->event.lun[1] = tpg->tport_tpgt & 0xFF;
1239 if (lun->unpacked_lun >= 256)
1240 evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
1241 evt->event.lun[3] = lun->unpacked_lun & 0xFF;
1242 }
1243
1244 llist_add(&evt->list, &vs->vs_event_list);
1245 vhost_work_queue(&vs->dev, &vs->vs_event_work);
1246}
1247
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001248static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
1249{
Asias Hea6c9af82013-04-25 15:35:21 +08001250 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1251 poll.work);
1252 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1253
1254 mutex_lock(&vq->mutex);
1255 if (!vq->private_data)
1256 goto out;
1257
1258 if (vs->vs_events_missed)
1259 tcm_vhost_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
1260out:
1261 mutex_unlock(&vq->mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001262}
1263
1264static void vhost_scsi_handle_kick(struct vhost_work *work)
1265{
1266 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1267 poll.work);
1268 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1269
Asias He1b7f3902013-02-06 13:20:59 +08001270 vhost_scsi_handle_vq(vs, vq);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001271}
1272
Asias He4f7f46d2013-04-03 14:17:37 +08001273static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
1274{
Asias He3ab2e422013-04-27 11:16:48 +08001275 vhost_poll_flush(&vs->vqs[index].vq.poll);
Asias He4f7f46d2013-04-03 14:17:37 +08001276}
1277
Michael S. Tsirkin3dfbff32013-04-28 15:38:52 +03001278/* Callers must hold dev mutex */
Asias He4f7f46d2013-04-03 14:17:37 +08001279static void vhost_scsi_flush(struct vhost_scsi *vs)
1280{
Asias Hef2f0173d2013-04-27 11:16:49 +08001281 struct vhost_scsi_inflight *old_inflight[VHOST_SCSI_MAX_VQ];
Asias He4f7f46d2013-04-03 14:17:37 +08001282 int i;
1283
Asias Hef2f0173d2013-04-27 11:16:49 +08001284 /* Init new inflight and remember the old inflight */
1285 tcm_vhost_init_inflight(vs, old_inflight);
1286
1287 /*
1288 * The inflight->kref was initialized to 1. We decrement it here to
1289 * indicate the start of the flush operation so that it will reach 0
1290 * when all the reqs are finished.
1291 */
1292 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1293 kref_put(&old_inflight[i]->kref, tcm_vhost_done_inflight);
1294
1295 /* Flush both the vhost poll and vhost work */
Asias He4f7f46d2013-04-03 14:17:37 +08001296 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1297 vhost_scsi_flush_vq(vs, i);
1298 vhost_work_flush(&vs->dev, &vs->vs_completion_work);
Asias Hea6c9af82013-04-25 15:35:21 +08001299 vhost_work_flush(&vs->dev, &vs->vs_event_work);
Asias Hef2f0173d2013-04-27 11:16:49 +08001300
1301 /* Wait for all reqs issued before the flush to be finished */
1302 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1303 wait_for_completion(&old_inflight[i]->comp);
Asias He4f7f46d2013-04-03 14:17:37 +08001304}
1305
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001306/*
1307 * Called from vhost_scsi_ioctl() context to walk the list of available
1308 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
Asias Hef2b7daf2013-04-25 15:35:20 +08001309 *
1310 * The lock nesting rule is:
1311 * tcm_vhost_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001312 */
Asias He683bd962013-05-06 16:38:27 +08001313static int
1314vhost_scsi_set_endpoint(struct vhost_scsi *vs,
1315 struct vhost_scsi_target *t)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001316{
Nicholas Bellingerab8edab2014-10-08 06:19:20 +00001317 struct se_portal_group *se_tpg;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001318 struct tcm_vhost_tport *tv_tport;
Asias He98718312013-05-06 16:38:28 +08001319 struct tcm_vhost_tpg *tpg;
Asias He4f7f46d2013-04-03 14:17:37 +08001320 struct tcm_vhost_tpg **vs_tpg;
1321 struct vhost_virtqueue *vq;
1322 int index, ret, i, len;
Asias He67e18cf2013-02-05 12:31:57 +08001323 bool match = false;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001324
Asias Hef2b7daf2013-04-25 15:35:20 +08001325 mutex_lock(&tcm_vhost_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001326 mutex_lock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001327
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001328 /* Verify that ring has been setup correctly. */
1329 for (index = 0; index < vs->dev.nvqs; ++index) {
1330 /* Verify that ring has been setup correctly. */
Asias He3ab2e422013-04-27 11:16:48 +08001331 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
Asias Hef2b7daf2013-04-25 15:35:20 +08001332 ret = -EFAULT;
1333 goto out;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001334 }
1335 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001336
Asias He4f7f46d2013-04-03 14:17:37 +08001337 len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
1338 vs_tpg = kzalloc(len, GFP_KERNEL);
1339 if (!vs_tpg) {
Asias Hef2b7daf2013-04-25 15:35:20 +08001340 ret = -ENOMEM;
1341 goto out;
Asias He4f7f46d2013-04-03 14:17:37 +08001342 }
1343 if (vs->vs_tpg)
1344 memcpy(vs_tpg, vs->vs_tpg, len);
1345
Asias He98718312013-05-06 16:38:28 +08001346 list_for_each_entry(tpg, &tcm_vhost_list, tv_tpg_list) {
1347 mutex_lock(&tpg->tv_tpg_mutex);
1348 if (!tpg->tpg_nexus) {
1349 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001350 continue;
1351 }
Asias He98718312013-05-06 16:38:28 +08001352 if (tpg->tv_tpg_vhost_count != 0) {
1353 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001354 continue;
1355 }
Asias He98718312013-05-06 16:38:28 +08001356 tv_tport = tpg->tport;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001357
Asias He67e18cf2013-02-05 12:31:57 +08001358 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
Asias He98718312013-05-06 16:38:28 +08001359 if (vs->vs_tpg && vs->vs_tpg[tpg->tport_tpgt]) {
Asias He4f7f46d2013-04-03 14:17:37 +08001360 kfree(vs_tpg);
Asias He98718312013-05-06 16:38:28 +08001361 mutex_unlock(&tpg->tv_tpg_mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001362 ret = -EEXIST;
1363 goto out;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001364 }
Nicholas Bellingerab8edab2014-10-08 06:19:20 +00001365 /*
1366 * In order to ensure individual vhost-scsi configfs
1367 * groups cannot be removed while in use by vhost ioctl,
1368 * go ahead and take an explicit se_tpg->tpg_group.cg_item
1369 * dependency now.
1370 */
1371 se_tpg = &tpg->se_tpg;
1372 ret = configfs_depend_item(se_tpg->se_tpg_tfo->tf_subsys,
1373 &se_tpg->tpg_group.cg_item);
1374 if (ret) {
1375 pr_warn("configfs_depend_item() failed: %d\n", ret);
1376 kfree(vs_tpg);
1377 mutex_unlock(&tpg->tv_tpg_mutex);
1378 goto out;
1379 }
Asias He98718312013-05-06 16:38:28 +08001380 tpg->tv_tpg_vhost_count++;
1381 tpg->vhost_scsi = vs;
1382 vs_tpg[tpg->tport_tpgt] = tpg;
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001383 smp_mb__after_atomic();
Asias He67e18cf2013-02-05 12:31:57 +08001384 match = true;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001385 }
Asias He98718312013-05-06 16:38:28 +08001386 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001387 }
Asias He67e18cf2013-02-05 12:31:57 +08001388
1389 if (match) {
1390 memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
1391 sizeof(vs->vs_vhost_wwpn));
Asias He4f7f46d2013-04-03 14:17:37 +08001392 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
Asias He3ab2e422013-04-27 11:16:48 +08001393 vq = &vs->vqs[i].vq;
Asias He4f7f46d2013-04-03 14:17:37 +08001394 mutex_lock(&vq->mutex);
Asias He22fa90c2013-05-07 14:54:36 +08001395 vq->private_data = vs_tpg;
Asias Hedfd5d562013-04-03 14:17:38 +08001396 vhost_init_used(vq);
Asias He4f7f46d2013-04-03 14:17:37 +08001397 mutex_unlock(&vq->mutex);
1398 }
Asias He67e18cf2013-02-05 12:31:57 +08001399 ret = 0;
1400 } else {
1401 ret = -EEXIST;
1402 }
1403
Asias He4f7f46d2013-04-03 14:17:37 +08001404 /*
1405 * Act as synchronize_rcu to make sure access to
1406 * old vs->vs_tpg is finished.
1407 */
1408 vhost_scsi_flush(vs);
1409 kfree(vs->vs_tpg);
1410 vs->vs_tpg = vs_tpg;
1411
Asias Hef2b7daf2013-04-25 15:35:20 +08001412out:
Asias He67e18cf2013-02-05 12:31:57 +08001413 mutex_unlock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001414 mutex_unlock(&tcm_vhost_mutex);
Asias He67e18cf2013-02-05 12:31:57 +08001415 return ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001416}
1417
Asias He683bd962013-05-06 16:38:27 +08001418static int
1419vhost_scsi_clear_endpoint(struct vhost_scsi *vs,
1420 struct vhost_scsi_target *t)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001421{
Nicholas Bellingerab8edab2014-10-08 06:19:20 +00001422 struct se_portal_group *se_tpg;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001423 struct tcm_vhost_tport *tv_tport;
Asias He98718312013-05-06 16:38:28 +08001424 struct tcm_vhost_tpg *tpg;
Asias He4f7f46d2013-04-03 14:17:37 +08001425 struct vhost_virtqueue *vq;
1426 bool match = false;
Asias He67e18cf2013-02-05 12:31:57 +08001427 int index, ret, i;
1428 u8 target;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001429
Asias Hef2b7daf2013-04-25 15:35:20 +08001430 mutex_lock(&tcm_vhost_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001431 mutex_lock(&vs->dev.mutex);
1432 /* Verify that ring has been setup correctly. */
1433 for (index = 0; index < vs->dev.nvqs; ++index) {
Asias He3ab2e422013-04-27 11:16:48 +08001434 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001435 ret = -EFAULT;
Asias He038e0af42013-03-15 09:14:05 +08001436 goto err_dev;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001437 }
1438 }
Asias He4f7f46d2013-04-03 14:17:37 +08001439
1440 if (!vs->vs_tpg) {
Asias Hef2b7daf2013-04-25 15:35:20 +08001441 ret = 0;
1442 goto err_dev;
Asias He4f7f46d2013-04-03 14:17:37 +08001443 }
1444
Asias He67e18cf2013-02-05 12:31:57 +08001445 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
1446 target = i;
Asias He98718312013-05-06 16:38:28 +08001447 tpg = vs->vs_tpg[target];
1448 if (!tpg)
Asias He67e18cf2013-02-05 12:31:57 +08001449 continue;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001450
Asias He98718312013-05-06 16:38:28 +08001451 mutex_lock(&tpg->tv_tpg_mutex);
1452 tv_tport = tpg->tport;
Asias He67e18cf2013-02-05 12:31:57 +08001453 if (!tv_tport) {
1454 ret = -ENODEV;
Asias He038e0af42013-03-15 09:14:05 +08001455 goto err_tpg;
Asias He67e18cf2013-02-05 12:31:57 +08001456 }
1457
1458 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
Asias He98718312013-05-06 16:38:28 +08001459 pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu"
Asias He67e18cf2013-02-05 12:31:57 +08001460 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
Asias He98718312013-05-06 16:38:28 +08001461 tv_tport->tport_name, tpg->tport_tpgt,
Asias He67e18cf2013-02-05 12:31:57 +08001462 t->vhost_wwpn, t->vhost_tpgt);
1463 ret = -EINVAL;
Asias He038e0af42013-03-15 09:14:05 +08001464 goto err_tpg;
Asias He67e18cf2013-02-05 12:31:57 +08001465 }
Asias He98718312013-05-06 16:38:28 +08001466 tpg->tv_tpg_vhost_count--;
1467 tpg->vhost_scsi = NULL;
Asias He67e18cf2013-02-05 12:31:57 +08001468 vs->vs_tpg[target] = NULL;
Asias He4f7f46d2013-04-03 14:17:37 +08001469 match = true;
Asias He98718312013-05-06 16:38:28 +08001470 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellingerab8edab2014-10-08 06:19:20 +00001471 /*
1472 * Release se_tpg->tpg_group.cg_item configfs dependency now
1473 * to allow vhost-scsi WWPN se_tpg->tpg_group shutdown to occur.
1474 */
1475 se_tpg = &tpg->se_tpg;
1476 configfs_undepend_item(se_tpg->se_tpg_tfo->tf_subsys,
1477 &se_tpg->tpg_group.cg_item);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001478 }
Asias He4f7f46d2013-04-03 14:17:37 +08001479 if (match) {
1480 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
Asias He3ab2e422013-04-27 11:16:48 +08001481 vq = &vs->vqs[i].vq;
Asias He4f7f46d2013-04-03 14:17:37 +08001482 mutex_lock(&vq->mutex);
Asias He22fa90c2013-05-07 14:54:36 +08001483 vq->private_data = NULL;
Asias He4f7f46d2013-04-03 14:17:37 +08001484 mutex_unlock(&vq->mutex);
1485 }
1486 }
1487 /*
1488 * Act as synchronize_rcu to make sure access to
1489 * old vs->vs_tpg is finished.
1490 */
1491 vhost_scsi_flush(vs);
1492 kfree(vs->vs_tpg);
1493 vs->vs_tpg = NULL;
Asias Hea6c9af82013-04-25 15:35:21 +08001494 WARN_ON(vs->vs_events_nr);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001495 mutex_unlock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001496 mutex_unlock(&tcm_vhost_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001497 return 0;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001498
Asias He038e0af42013-03-15 09:14:05 +08001499err_tpg:
Asias He98718312013-05-06 16:38:28 +08001500 mutex_unlock(&tpg->tv_tpg_mutex);
Asias He038e0af42013-03-15 09:14:05 +08001501err_dev:
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001502 mutex_unlock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001503 mutex_unlock(&tcm_vhost_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001504 return ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001505}
1506
Asias He4f7f46d2013-04-03 14:17:37 +08001507static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
1508{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001509 struct vhost_virtqueue *vq;
1510 int i;
1511
Asias He4f7f46d2013-04-03 14:17:37 +08001512 if (features & ~VHOST_SCSI_FEATURES)
1513 return -EOPNOTSUPP;
1514
1515 mutex_lock(&vs->dev.mutex);
1516 if ((features & (1 << VHOST_F_LOG_ALL)) &&
1517 !vhost_log_access_ok(&vs->dev)) {
1518 mutex_unlock(&vs->dev.mutex);
1519 return -EFAULT;
1520 }
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001521
1522 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1523 vq = &vs->vqs[i].vq;
1524 mutex_lock(&vq->mutex);
1525 vq->acked_features = features;
1526 mutex_unlock(&vq->mutex);
1527 }
Asias He4f7f46d2013-04-03 14:17:37 +08001528 mutex_unlock(&vs->dev.mutex);
1529 return 0;
1530}
1531
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001532static int vhost_scsi_open(struct inode *inode, struct file *f)
1533{
Asias Hec7289312013-05-06 16:38:26 +08001534 struct vhost_scsi *vs;
Asias He3ab2e422013-04-27 11:16:48 +08001535 struct vhost_virtqueue **vqs;
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001536 int r = -ENOMEM, i;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001537
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001538 vs = kzalloc(sizeof(*vs), GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
1539 if (!vs) {
1540 vs = vzalloc(sizeof(*vs));
1541 if (!vs)
1542 goto err_vs;
1543 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001544
Asias He3ab2e422013-04-27 11:16:48 +08001545 vqs = kmalloc(VHOST_SCSI_MAX_VQ * sizeof(*vqs), GFP_KERNEL);
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001546 if (!vqs)
1547 goto err_vqs;
Asias He3ab2e422013-04-27 11:16:48 +08001548
Asias Hec7289312013-05-06 16:38:26 +08001549 vhost_work_init(&vs->vs_completion_work, vhost_scsi_complete_cmd_work);
1550 vhost_work_init(&vs->vs_event_work, tcm_vhost_evt_work);
Asias Hea6c9af82013-04-25 15:35:21 +08001551
Asias Hec7289312013-05-06 16:38:26 +08001552 vs->vs_events_nr = 0;
1553 vs->vs_events_missed = false;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001554
Asias Hec7289312013-05-06 16:38:26 +08001555 vqs[VHOST_SCSI_VQ_CTL] = &vs->vqs[VHOST_SCSI_VQ_CTL].vq;
1556 vqs[VHOST_SCSI_VQ_EVT] = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1557 vs->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick;
1558 vs->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick;
Asias He3ab2e422013-04-27 11:16:48 +08001559 for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) {
Asias Hec7289312013-05-06 16:38:26 +08001560 vqs[i] = &vs->vqs[i].vq;
1561 vs->vqs[i].vq.handle_kick = vhost_scsi_handle_kick;
Asias He3ab2e422013-04-27 11:16:48 +08001562 }
Zhi Yong Wu59566b6e2013-12-07 04:13:03 +08001563 vhost_dev_init(&vs->dev, vqs, VHOST_SCSI_MAX_VQ);
Asias Hef2f0173d2013-04-27 11:16:49 +08001564
Asias Hec7289312013-05-06 16:38:26 +08001565 tcm_vhost_init_inflight(vs, NULL);
Asias Hef2f0173d2013-04-27 11:16:49 +08001566
Asias Hec7289312013-05-06 16:38:26 +08001567 f->private_data = vs;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001568 return 0;
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001569
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001570err_vqs:
Michael S. Tsirkin68404442014-06-12 19:00:01 +03001571 kvfree(vs);
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001572err_vs:
1573 return r;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001574}
1575
1576static int vhost_scsi_release(struct inode *inode, struct file *f)
1577{
Asias Hec7289312013-05-06 16:38:26 +08001578 struct vhost_scsi *vs = f->private_data;
Asias He67e18cf2013-02-05 12:31:57 +08001579 struct vhost_scsi_target t;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001580
Asias Hec7289312013-05-06 16:38:26 +08001581 mutex_lock(&vs->dev.mutex);
1582 memcpy(t.vhost_wwpn, vs->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
1583 mutex_unlock(&vs->dev.mutex);
1584 vhost_scsi_clear_endpoint(vs, &t);
1585 vhost_dev_stop(&vs->dev);
1586 vhost_dev_cleanup(&vs->dev, false);
Asias Hea6c9af82013-04-25 15:35:21 +08001587 /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
Asias Hec7289312013-05-06 16:38:26 +08001588 vhost_scsi_flush(vs);
1589 kfree(vs->dev.vqs);
Michael S. Tsirkin68404442014-06-12 19:00:01 +03001590 kvfree(vs);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001591 return 0;
1592}
1593
Asias He683bd962013-05-06 16:38:27 +08001594static long
1595vhost_scsi_ioctl(struct file *f,
1596 unsigned int ioctl,
1597 unsigned long arg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001598{
1599 struct vhost_scsi *vs = f->private_data;
1600 struct vhost_scsi_target backend;
1601 void __user *argp = (void __user *)arg;
1602 u64 __user *featurep = argp;
Asias He11c63412013-04-25 15:35:22 +08001603 u32 __user *eventsp = argp;
1604 u32 events_missed;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001605 u64 features;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001606 int r, abi_version = VHOST_SCSI_ABI_VERSION;
Asias He3ab2e422013-04-27 11:16:48 +08001607 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001608
1609 switch (ioctl) {
1610 case VHOST_SCSI_SET_ENDPOINT:
1611 if (copy_from_user(&backend, argp, sizeof backend))
1612 return -EFAULT;
Michael S. Tsirkin6de71452012-08-18 15:44:09 -07001613 if (backend.reserved != 0)
1614 return -EOPNOTSUPP;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001615
1616 return vhost_scsi_set_endpoint(vs, &backend);
1617 case VHOST_SCSI_CLEAR_ENDPOINT:
1618 if (copy_from_user(&backend, argp, sizeof backend))
1619 return -EFAULT;
Michael S. Tsirkin6de71452012-08-18 15:44:09 -07001620 if (backend.reserved != 0)
1621 return -EOPNOTSUPP;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001622
1623 return vhost_scsi_clear_endpoint(vs, &backend);
1624 case VHOST_SCSI_GET_ABI_VERSION:
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001625 if (copy_to_user(argp, &abi_version, sizeof abi_version))
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001626 return -EFAULT;
1627 return 0;
Asias He11c63412013-04-25 15:35:22 +08001628 case VHOST_SCSI_SET_EVENTS_MISSED:
1629 if (get_user(events_missed, eventsp))
1630 return -EFAULT;
1631 mutex_lock(&vq->mutex);
1632 vs->vs_events_missed = events_missed;
1633 mutex_unlock(&vq->mutex);
1634 return 0;
1635 case VHOST_SCSI_GET_EVENTS_MISSED:
1636 mutex_lock(&vq->mutex);
1637 events_missed = vs->vs_events_missed;
1638 mutex_unlock(&vq->mutex);
1639 if (put_user(events_missed, eventsp))
1640 return -EFAULT;
1641 return 0;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001642 case VHOST_GET_FEATURES:
Nicholas Bellinger5dade712013-03-27 17:23:41 -07001643 features = VHOST_SCSI_FEATURES;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001644 if (copy_to_user(featurep, &features, sizeof features))
1645 return -EFAULT;
1646 return 0;
1647 case VHOST_SET_FEATURES:
1648 if (copy_from_user(&features, featurep, sizeof features))
1649 return -EFAULT;
1650 return vhost_scsi_set_features(vs, features);
1651 default:
1652 mutex_lock(&vs->dev.mutex);
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001653 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
1654 /* TODO: flush backend after dev ioctl. */
1655 if (r == -ENOIOCTLCMD)
1656 r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001657 mutex_unlock(&vs->dev.mutex);
1658 return r;
1659 }
1660}
1661
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001662#ifdef CONFIG_COMPAT
1663static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
1664 unsigned long arg)
1665{
1666 return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1667}
1668#endif
1669
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001670static const struct file_operations vhost_scsi_fops = {
1671 .owner = THIS_MODULE,
1672 .release = vhost_scsi_release,
1673 .unlocked_ioctl = vhost_scsi_ioctl,
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001674#ifdef CONFIG_COMPAT
1675 .compat_ioctl = vhost_scsi_compat_ioctl,
1676#endif
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001677 .open = vhost_scsi_open,
1678 .llseek = noop_llseek,
1679};
1680
1681static struct miscdevice vhost_scsi_misc = {
1682 MISC_DYNAMIC_MINOR,
1683 "vhost-scsi",
1684 &vhost_scsi_fops,
1685};
1686
1687static int __init vhost_scsi_register(void)
1688{
1689 return misc_register(&vhost_scsi_misc);
1690}
1691
1692static int vhost_scsi_deregister(void)
1693{
1694 return misc_deregister(&vhost_scsi_misc);
1695}
1696
1697static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
1698{
1699 switch (tport->tport_proto_id) {
1700 case SCSI_PROTOCOL_SAS:
1701 return "SAS";
1702 case SCSI_PROTOCOL_FCP:
1703 return "FCP";
1704 case SCSI_PROTOCOL_ISCSI:
1705 return "iSCSI";
1706 default:
1707 break;
1708 }
1709
1710 return "Unknown";
1711}
1712
Asias He683bd962013-05-06 16:38:27 +08001713static void
1714tcm_vhost_do_plug(struct tcm_vhost_tpg *tpg,
1715 struct se_lun *lun, bool plug)
Asias Hea6c9af82013-04-25 15:35:21 +08001716{
1717
1718 struct vhost_scsi *vs = tpg->vhost_scsi;
1719 struct vhost_virtqueue *vq;
1720 u32 reason;
1721
1722 if (!vs)
1723 return;
1724
1725 mutex_lock(&vs->dev.mutex);
Asias Hea6c9af82013-04-25 15:35:21 +08001726
1727 if (plug)
1728 reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
1729 else
1730 reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
1731
Asias He3ab2e422013-04-27 11:16:48 +08001732 vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Asias Hea6c9af82013-04-25 15:35:21 +08001733 mutex_lock(&vq->mutex);
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001734 if (vhost_has_feature(vq, VIRTIO_SCSI_F_HOTPLUG))
1735 tcm_vhost_send_evt(vs, tpg, lun,
1736 VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
Asias Hea6c9af82013-04-25 15:35:21 +08001737 mutex_unlock(&vq->mutex);
1738 mutex_unlock(&vs->dev.mutex);
1739}
1740
1741static void tcm_vhost_hotplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1742{
1743 tcm_vhost_do_plug(tpg, lun, true);
1744}
1745
1746static void tcm_vhost_hotunplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1747{
1748 tcm_vhost_do_plug(tpg, lun, false);
1749}
1750
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001751static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08001752 struct se_lun *lun)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001753{
Asias He98718312013-05-06 16:38:28 +08001754 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001755 struct tcm_vhost_tpg, se_tpg);
1756
Asias Hea6c9af82013-04-25 15:35:21 +08001757 mutex_lock(&tcm_vhost_mutex);
1758
Asias He98718312013-05-06 16:38:28 +08001759 mutex_lock(&tpg->tv_tpg_mutex);
1760 tpg->tv_tpg_port_count++;
1761 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001762
Asias He98718312013-05-06 16:38:28 +08001763 tcm_vhost_hotplug(tpg, lun);
Asias Hea6c9af82013-04-25 15:35:21 +08001764
1765 mutex_unlock(&tcm_vhost_mutex);
1766
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001767 return 0;
1768}
1769
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001770static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08001771 struct se_lun *lun)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001772{
Asias He98718312013-05-06 16:38:28 +08001773 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001774 struct tcm_vhost_tpg, se_tpg);
1775
Asias Hea6c9af82013-04-25 15:35:21 +08001776 mutex_lock(&tcm_vhost_mutex);
1777
Asias He98718312013-05-06 16:38:28 +08001778 mutex_lock(&tpg->tv_tpg_mutex);
1779 tpg->tv_tpg_port_count--;
1780 mutex_unlock(&tpg->tv_tpg_mutex);
Asias Hea6c9af82013-04-25 15:35:21 +08001781
Asias He98718312013-05-06 16:38:28 +08001782 tcm_vhost_hotunplug(tpg, lun);
Asias Hea6c9af82013-04-25 15:35:21 +08001783
1784 mutex_unlock(&tcm_vhost_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001785}
1786
Asias He683bd962013-05-06 16:38:27 +08001787static struct se_node_acl *
1788tcm_vhost_make_nodeacl(struct se_portal_group *se_tpg,
1789 struct config_group *group,
1790 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001791{
1792 struct se_node_acl *se_nacl, *se_nacl_new;
1793 struct tcm_vhost_nacl *nacl;
1794 u64 wwpn = 0;
1795 u32 nexus_depth;
1796
1797 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1798 return ERR_PTR(-EINVAL); */
1799 se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
1800 if (!se_nacl_new)
1801 return ERR_PTR(-ENOMEM);
1802
1803 nexus_depth = 1;
1804 /*
1805 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1806 * when converting a NodeACL from demo mode -> explict
1807 */
1808 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1809 name, nexus_depth);
1810 if (IS_ERR(se_nacl)) {
1811 tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
1812 return se_nacl;
1813 }
1814 /*
1815 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1816 */
1817 nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
1818 nacl->iport_wwpn = wwpn;
1819
1820 return se_nacl;
1821}
1822
1823static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
1824{
1825 struct tcm_vhost_nacl *nacl = container_of(se_acl,
1826 struct tcm_vhost_nacl, se_node_acl);
1827 core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1828 kfree(nacl);
1829}
1830
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001831static void tcm_vhost_free_cmd_map_res(struct tcm_vhost_nexus *nexus,
1832 struct se_session *se_sess)
1833{
1834 struct tcm_vhost_cmd *tv_cmd;
1835 unsigned int i;
1836
1837 if (!se_sess->sess_cmd_map)
1838 return;
1839
1840 for (i = 0; i < TCM_VHOST_DEFAULT_TAGS; i++) {
1841 tv_cmd = &((struct tcm_vhost_cmd *)se_sess->sess_cmd_map)[i];
1842
1843 kfree(tv_cmd->tvc_sgl);
Nicholas Bellingerb1935f62014-02-22 18:08:24 -08001844 kfree(tv_cmd->tvc_prot_sgl);
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001845 kfree(tv_cmd->tvc_upages);
1846 }
1847}
1848
Asias He98718312013-05-06 16:38:28 +08001849static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tpg,
Asias He683bd962013-05-06 16:38:27 +08001850 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001851{
1852 struct se_portal_group *se_tpg;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001853 struct se_session *se_sess;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001854 struct tcm_vhost_nexus *tv_nexus;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001855 struct tcm_vhost_cmd *tv_cmd;
1856 unsigned int i;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001857
Asias He98718312013-05-06 16:38:28 +08001858 mutex_lock(&tpg->tv_tpg_mutex);
1859 if (tpg->tpg_nexus) {
1860 mutex_unlock(&tpg->tv_tpg_mutex);
1861 pr_debug("tpg->tpg_nexus already exists\n");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001862 return -EEXIST;
1863 }
Asias He98718312013-05-06 16:38:28 +08001864 se_tpg = &tpg->se_tpg;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001865
1866 tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
1867 if (!tv_nexus) {
Asias He98718312013-05-06 16:38:28 +08001868 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001869 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1870 return -ENOMEM;
1871 }
1872 /*
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -07001873 * Initialize the struct se_session pointer and setup tagpool
1874 * for struct tcm_vhost_cmd descriptors
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001875 */
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -07001876 tv_nexus->tvn_se_sess = transport_init_session_tags(
1877 TCM_VHOST_DEFAULT_TAGS,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07001878 sizeof(struct tcm_vhost_cmd),
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001879 TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001880 if (IS_ERR(tv_nexus->tvn_se_sess)) {
Asias He98718312013-05-06 16:38:28 +08001881 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001882 kfree(tv_nexus);
1883 return -ENOMEM;
1884 }
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001885 se_sess = tv_nexus->tvn_se_sess;
1886 for (i = 0; i < TCM_VHOST_DEFAULT_TAGS; i++) {
1887 tv_cmd = &((struct tcm_vhost_cmd *)se_sess->sess_cmd_map)[i];
1888
1889 tv_cmd->tvc_sgl = kzalloc(sizeof(struct scatterlist) *
1890 TCM_VHOST_PREALLOC_SGLS, GFP_KERNEL);
1891 if (!tv_cmd->tvc_sgl) {
1892 mutex_unlock(&tpg->tv_tpg_mutex);
1893 pr_err("Unable to allocate tv_cmd->tvc_sgl\n");
1894 goto out;
1895 }
1896
1897 tv_cmd->tvc_upages = kzalloc(sizeof(struct page *) *
Nicholas Bellinger5a01d082014-02-22 18:34:08 -08001898 TCM_VHOST_PREALLOC_UPAGES, GFP_KERNEL);
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001899 if (!tv_cmd->tvc_upages) {
1900 mutex_unlock(&tpg->tv_tpg_mutex);
1901 pr_err("Unable to allocate tv_cmd->tvc_upages\n");
1902 goto out;
1903 }
Nicholas Bellingerb1935f62014-02-22 18:08:24 -08001904
1905 tv_cmd->tvc_prot_sgl = kzalloc(sizeof(struct scatterlist) *
1906 TCM_VHOST_PREALLOC_PROT_SGLS, GFP_KERNEL);
1907 if (!tv_cmd->tvc_prot_sgl) {
1908 mutex_unlock(&tpg->tv_tpg_mutex);
1909 pr_err("Unable to allocate tv_cmd->tvc_prot_sgl\n");
1910 goto out;
1911 }
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001912 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001913 /*
1914 * Since we are running in 'demo mode' this call with generate a
1915 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1916 * the SCSI Initiator port name of the passed configfs group 'name'.
1917 */
1918 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1919 se_tpg, (unsigned char *)name);
1920 if (!tv_nexus->tvn_se_sess->se_node_acl) {
Asias He98718312013-05-06 16:38:28 +08001921 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001922 pr_debug("core_tpg_check_initiator_node_acl() failed"
1923 " for %s\n", name);
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001924 goto out;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001925 }
1926 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001927 * Now register the TCM vhost virtual I_T Nexus as active with the
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001928 * call to __transport_register_session()
1929 */
1930 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1931 tv_nexus->tvn_se_sess, tv_nexus);
Asias He98718312013-05-06 16:38:28 +08001932 tpg->tpg_nexus = tv_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001933
Asias He98718312013-05-06 16:38:28 +08001934 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001935 return 0;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001936
1937out:
1938 tcm_vhost_free_cmd_map_res(tv_nexus, se_sess);
1939 transport_free_session(se_sess);
1940 kfree(tv_nexus);
1941 return -ENOMEM;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001942}
1943
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001944static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001945{
1946 struct se_session *se_sess;
1947 struct tcm_vhost_nexus *tv_nexus;
1948
1949 mutex_lock(&tpg->tv_tpg_mutex);
1950 tv_nexus = tpg->tpg_nexus;
1951 if (!tv_nexus) {
1952 mutex_unlock(&tpg->tv_tpg_mutex);
1953 return -ENODEV;
1954 }
1955
1956 se_sess = tv_nexus->tvn_se_sess;
1957 if (!se_sess) {
1958 mutex_unlock(&tpg->tv_tpg_mutex);
1959 return -ENODEV;
1960 }
1961
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001962 if (tpg->tv_tpg_port_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001963 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001964 pr_err("Unable to remove TCM_vhost I_T Nexus with"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001965 " active TPG port count: %d\n",
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001966 tpg->tv_tpg_port_count);
1967 return -EBUSY;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001968 }
1969
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001970 if (tpg->tv_tpg_vhost_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001971 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001972 pr_err("Unable to remove TCM_vhost I_T Nexus with"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001973 " active TPG vhost count: %d\n",
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001974 tpg->tv_tpg_vhost_count);
1975 return -EBUSY;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001976 }
1977
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001978 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001979 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
1980 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001981
1982 tcm_vhost_free_cmd_map_res(tv_nexus, se_sess);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001983 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001984 * Release the SCSI I_T Nexus to the emulated vhost Target Port
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001985 */
1986 transport_deregister_session(tv_nexus->tvn_se_sess);
1987 tpg->tpg_nexus = NULL;
1988 mutex_unlock(&tpg->tv_tpg_mutex);
1989
1990 kfree(tv_nexus);
1991 return 0;
1992}
1993
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001994static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08001995 char *page)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001996{
Asias He98718312013-05-06 16:38:28 +08001997 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001998 struct tcm_vhost_tpg, se_tpg);
1999 struct tcm_vhost_nexus *tv_nexus;
2000 ssize_t ret;
2001
Asias He98718312013-05-06 16:38:28 +08002002 mutex_lock(&tpg->tv_tpg_mutex);
2003 tv_nexus = tpg->tpg_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002004 if (!tv_nexus) {
Asias He98718312013-05-06 16:38:28 +08002005 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002006 return -ENODEV;
2007 }
2008 ret = snprintf(page, PAGE_SIZE, "%s\n",
2009 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
Asias He98718312013-05-06 16:38:28 +08002010 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002011
2012 return ret;
2013}
2014
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002015static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08002016 const char *page,
2017 size_t count)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002018{
Asias He98718312013-05-06 16:38:28 +08002019 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002020 struct tcm_vhost_tpg, se_tpg);
Asias He98718312013-05-06 16:38:28 +08002021 struct tcm_vhost_tport *tport_wwn = tpg->tport;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002022 unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
2023 int ret;
2024 /*
2025 * Shutdown the active I_T nexus if 'NULL' is passed..
2026 */
2027 if (!strncmp(page, "NULL", 4)) {
Asias He98718312013-05-06 16:38:28 +08002028 ret = tcm_vhost_drop_nexus(tpg);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002029 return (!ret) ? count : ret;
2030 }
2031 /*
2032 * Otherwise make sure the passed virtual Initiator port WWN matches
2033 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
2034 * tcm_vhost_make_nexus().
2035 */
2036 if (strlen(page) >= TCM_VHOST_NAMELEN) {
2037 pr_err("Emulated NAA Sas Address: %s, exceeds"
2038 " max: %d\n", page, TCM_VHOST_NAMELEN);
2039 return -EINVAL;
2040 }
2041 snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
2042
2043 ptr = strstr(i_port, "naa.");
2044 if (ptr) {
2045 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
2046 pr_err("Passed SAS Initiator Port %s does not"
2047 " match target port protoid: %s\n", i_port,
2048 tcm_vhost_dump_proto_id(tport_wwn));
2049 return -EINVAL;
2050 }
2051 port_ptr = &i_port[0];
2052 goto check_newline;
2053 }
2054 ptr = strstr(i_port, "fc.");
2055 if (ptr) {
2056 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
2057 pr_err("Passed FCP Initiator Port %s does not"
2058 " match target port protoid: %s\n", i_port,
2059 tcm_vhost_dump_proto_id(tport_wwn));
2060 return -EINVAL;
2061 }
2062 port_ptr = &i_port[3]; /* Skip over "fc." */
2063 goto check_newline;
2064 }
2065 ptr = strstr(i_port, "iqn.");
2066 if (ptr) {
2067 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
2068 pr_err("Passed iSCSI Initiator Port %s does not"
2069 " match target port protoid: %s\n", i_port,
2070 tcm_vhost_dump_proto_id(tport_wwn));
2071 return -EINVAL;
2072 }
2073 port_ptr = &i_port[0];
2074 goto check_newline;
2075 }
2076 pr_err("Unable to locate prefix for emulated Initiator Port:"
2077 " %s\n", i_port);
2078 return -EINVAL;
2079 /*
2080 * Clear any trailing newline for the NAA WWN
2081 */
2082check_newline:
2083 if (i_port[strlen(i_port)-1] == '\n')
2084 i_port[strlen(i_port)-1] = '\0';
2085
Asias He98718312013-05-06 16:38:28 +08002086 ret = tcm_vhost_make_nexus(tpg, port_ptr);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002087 if (ret < 0)
2088 return ret;
2089
2090 return count;
2091}
2092
2093TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
2094
2095static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
2096 &tcm_vhost_tpg_nexus.attr,
2097 NULL,
2098};
2099
Asias He683bd962013-05-06 16:38:27 +08002100static struct se_portal_group *
2101tcm_vhost_make_tpg(struct se_wwn *wwn,
2102 struct config_group *group,
2103 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002104{
2105 struct tcm_vhost_tport *tport = container_of(wwn,
2106 struct tcm_vhost_tport, tport_wwn);
2107
2108 struct tcm_vhost_tpg *tpg;
2109 unsigned long tpgt;
2110 int ret;
2111
2112 if (strstr(name, "tpgt_") != name)
2113 return ERR_PTR(-EINVAL);
2114 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
2115 return ERR_PTR(-EINVAL);
2116
2117 tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
2118 if (!tpg) {
2119 pr_err("Unable to allocate struct tcm_vhost_tpg");
2120 return ERR_PTR(-ENOMEM);
2121 }
2122 mutex_init(&tpg->tv_tpg_mutex);
2123 INIT_LIST_HEAD(&tpg->tv_tpg_list);
2124 tpg->tport = tport;
2125 tpg->tport_tpgt = tpgt;
2126
2127 ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
2128 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
2129 if (ret < 0) {
2130 kfree(tpg);
2131 return NULL;
2132 }
2133 mutex_lock(&tcm_vhost_mutex);
2134 list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
2135 mutex_unlock(&tcm_vhost_mutex);
2136
2137 return &tpg->se_tpg;
2138}
2139
2140static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
2141{
2142 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
2143 struct tcm_vhost_tpg, se_tpg);
2144
2145 mutex_lock(&tcm_vhost_mutex);
2146 list_del(&tpg->tv_tpg_list);
2147 mutex_unlock(&tcm_vhost_mutex);
2148 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002149 * Release the virtual I_T Nexus for this vhost TPG
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002150 */
2151 tcm_vhost_drop_nexus(tpg);
2152 /*
2153 * Deregister the se_tpg from TCM..
2154 */
2155 core_tpg_deregister(se_tpg);
2156 kfree(tpg);
2157}
2158
Asias He683bd962013-05-06 16:38:27 +08002159static struct se_wwn *
2160tcm_vhost_make_tport(struct target_fabric_configfs *tf,
2161 struct config_group *group,
2162 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002163{
2164 struct tcm_vhost_tport *tport;
2165 char *ptr;
2166 u64 wwpn = 0;
2167 int off = 0;
2168
2169 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
2170 return ERR_PTR(-EINVAL); */
2171
2172 tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
2173 if (!tport) {
2174 pr_err("Unable to allocate struct tcm_vhost_tport");
2175 return ERR_PTR(-ENOMEM);
2176 }
2177 tport->tport_wwpn = wwpn;
2178 /*
2179 * Determine the emulated Protocol Identifier and Target Port Name
2180 * based on the incoming configfs directory name.
2181 */
2182 ptr = strstr(name, "naa.");
2183 if (ptr) {
2184 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
2185 goto check_len;
2186 }
2187 ptr = strstr(name, "fc.");
2188 if (ptr) {
2189 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
2190 off = 3; /* Skip over "fc." */
2191 goto check_len;
2192 }
2193 ptr = strstr(name, "iqn.");
2194 if (ptr) {
2195 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
2196 goto check_len;
2197 }
2198
2199 pr_err("Unable to locate prefix for emulated Target Port:"
2200 " %s\n", name);
2201 kfree(tport);
2202 return ERR_PTR(-EINVAL);
2203
2204check_len:
2205 if (strlen(name) >= TCM_VHOST_NAMELEN) {
2206 pr_err("Emulated %s Address: %s, exceeds"
2207 " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
2208 TCM_VHOST_NAMELEN);
2209 kfree(tport);
2210 return ERR_PTR(-EINVAL);
2211 }
2212 snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
2213
2214 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
2215 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
2216
2217 return &tport->tport_wwn;
2218}
2219
2220static void tcm_vhost_drop_tport(struct se_wwn *wwn)
2221{
2222 struct tcm_vhost_tport *tport = container_of(wwn,
2223 struct tcm_vhost_tport, tport_wwn);
2224
2225 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
2226 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
2227 tport->tport_name);
2228
2229 kfree(tport);
2230}
2231
Asias He683bd962013-05-06 16:38:27 +08002232static ssize_t
2233tcm_vhost_wwn_show_attr_version(struct target_fabric_configfs *tf,
2234 char *page)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002235{
2236 return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
2237 "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
2238 utsname()->machine);
2239}
2240
2241TF_WWN_ATTR_RO(tcm_vhost, version);
2242
2243static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
2244 &tcm_vhost_wwn_version.attr,
2245 NULL,
2246};
2247
2248static struct target_core_fabric_ops tcm_vhost_ops = {
2249 .get_fabric_name = tcm_vhost_get_fabric_name,
2250 .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
2251 .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
2252 .tpg_get_tag = tcm_vhost_get_tag,
2253 .tpg_get_default_depth = tcm_vhost_get_default_depth,
2254 .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
2255 .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
2256 .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
2257 .tpg_check_demo_mode = tcm_vhost_check_true,
2258 .tpg_check_demo_mode_cache = tcm_vhost_check_true,
2259 .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
2260 .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
2261 .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
2262 .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
2263 .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
2264 .release_cmd = tcm_vhost_release_cmd,
Nicholas Bellinger084ed452013-06-06 02:20:41 -07002265 .check_stop_free = vhost_scsi_check_stop_free,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002266 .shutdown_session = tcm_vhost_shutdown_session,
2267 .close_session = tcm_vhost_close_session,
2268 .sess_get_index = tcm_vhost_sess_get_index,
2269 .sess_get_initiator_sid = NULL,
2270 .write_pending = tcm_vhost_write_pending,
2271 .write_pending_status = tcm_vhost_write_pending_status,
2272 .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
2273 .get_task_tag = tcm_vhost_get_task_tag,
2274 .get_cmd_state = tcm_vhost_get_cmd_state,
2275 .queue_data_in = tcm_vhost_queue_data_in,
2276 .queue_status = tcm_vhost_queue_status,
2277 .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002278 .aborted_task = tcm_vhost_aborted_task,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002279 /*
2280 * Setup callers for generic logic in target_core_fabric_configfs.c
2281 */
2282 .fabric_make_wwn = tcm_vhost_make_tport,
2283 .fabric_drop_wwn = tcm_vhost_drop_tport,
2284 .fabric_make_tpg = tcm_vhost_make_tpg,
2285 .fabric_drop_tpg = tcm_vhost_drop_tpg,
2286 .fabric_post_link = tcm_vhost_port_link,
2287 .fabric_pre_unlink = tcm_vhost_port_unlink,
2288 .fabric_make_np = NULL,
2289 .fabric_drop_np = NULL,
2290 .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
2291 .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
2292};
2293
2294static int tcm_vhost_register_configfs(void)
2295{
2296 struct target_fabric_configfs *fabric;
2297 int ret;
2298
2299 pr_debug("TCM_VHOST fabric module %s on %s/%s"
2300 " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
2301 utsname()->machine);
2302 /*
2303 * Register the top level struct config_item_type with TCM core
2304 */
2305 fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
2306 if (IS_ERR(fabric)) {
2307 pr_err("target_fabric_configfs_init() failed\n");
2308 return PTR_ERR(fabric);
2309 }
2310 /*
2311 * Setup fabric->tf_ops from our local tcm_vhost_ops
2312 */
2313 fabric->tf_ops = tcm_vhost_ops;
2314 /*
2315 * Setup default attribute lists for various fabric->tf_cit_tmpl
2316 */
Andy Groverd80e224d2013-10-09 11:05:56 -07002317 fabric->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
2318 fabric->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
2319 fabric->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = NULL;
2320 fabric->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL;
2321 fabric->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
2322 fabric->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL;
2323 fabric->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
2324 fabric->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
2325 fabric->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002326 /*
2327 * Register the fabric for use within TCM
2328 */
2329 ret = target_fabric_configfs_register(fabric);
2330 if (ret < 0) {
2331 pr_err("target_fabric_configfs_register() failed"
2332 " for TCM_VHOST\n");
2333 return ret;
2334 }
2335 /*
2336 * Setup our local pointer to *fabric
2337 */
2338 tcm_vhost_fabric_configfs = fabric;
2339 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
2340 return 0;
2341};
2342
2343static void tcm_vhost_deregister_configfs(void)
2344{
2345 if (!tcm_vhost_fabric_configfs)
2346 return;
2347
2348 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
2349 tcm_vhost_fabric_configfs = NULL;
2350 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
2351};
2352
2353static int __init tcm_vhost_init(void)
2354{
2355 int ret = -ENOMEM;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002356 /*
2357 * Use our own dedicated workqueue for submitting I/O into
2358 * target core to avoid contention within system_wq.
2359 */
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002360 tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
2361 if (!tcm_vhost_workqueue)
2362 goto out;
2363
2364 ret = vhost_scsi_register();
2365 if (ret < 0)
2366 goto out_destroy_workqueue;
2367
2368 ret = tcm_vhost_register_configfs();
2369 if (ret < 0)
2370 goto out_vhost_scsi_deregister;
2371
2372 return 0;
2373
2374out_vhost_scsi_deregister:
2375 vhost_scsi_deregister();
2376out_destroy_workqueue:
2377 destroy_workqueue(tcm_vhost_workqueue);
2378out:
2379 return ret;
2380};
2381
2382static void tcm_vhost_exit(void)
2383{
2384 tcm_vhost_deregister_configfs();
2385 vhost_scsi_deregister();
2386 destroy_workqueue(tcm_vhost_workqueue);
2387};
2388
Michael S. Tsirkin181c04a2013-05-02 03:52:59 +03002389MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
2390MODULE_ALIAS("tcm_vhost");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002391MODULE_LICENSE("GPL");
2392module_init(tcm_vhost_init);
2393module_exit(tcm_vhost_exit);