blob: eabcf1875831e9a4e6bccfa9dbf93aec705ba25a [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
Nicholas Bellinger5dade712013-03-27 17:23:41 -0700171enum {
Asias Hea18cc422013-05-07 14:51:49 +0800172 VHOST_SCSI_FEATURES = VHOST_FEATURES | (1ULL << VIRTIO_SCSI_F_HOTPLUG)
Nicholas Bellinger5dade712013-03-27 17:23:41 -0700173};
174
Asias He1b7f3902013-02-06 13:20:59 +0800175#define VHOST_SCSI_MAX_TARGET 256
176#define VHOST_SCSI_MAX_VQ 128
Asias Hea6c9af82013-04-25 15:35:21 +0800177#define VHOST_SCSI_MAX_EVENT 128
Asias He67e18cf2013-02-05 12:31:57 +0800178
Asias He3ab2e422013-04-27 11:16:48 +0800179struct vhost_scsi_virtqueue {
180 struct vhost_virtqueue vq;
Michael S. Tsirkin3dfbff32013-04-28 15:38:52 +0300181 /*
182 * Reference counting for inflight reqs, used for flush operation. At
183 * each time, one reference tracks new commands submitted, while we
184 * wait for another one to reach 0.
185 */
Asias Hef2f0173d2013-04-27 11:16:49 +0800186 struct vhost_scsi_inflight inflights[2];
Michael S. Tsirkin3dfbff32013-04-28 15:38:52 +0300187 /*
188 * Indicate current inflight in use, protected by vq->mutex.
189 * Writers must also take dev mutex and flush under it.
190 */
Asias Hef2f0173d2013-04-27 11:16:49 +0800191 int inflight_idx;
Asias He3ab2e422013-04-27 11:16:48 +0800192};
193
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700194struct vhost_scsi {
Asias He67e18cf2013-02-05 12:31:57 +0800195 /* Protected by vhost_scsi->dev.mutex */
Asias He4f7f46d2013-04-03 14:17:37 +0800196 struct tcm_vhost_tpg **vs_tpg;
Asias He67e18cf2013-02-05 12:31:57 +0800197 char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
Asias He67e18cf2013-02-05 12:31:57 +0800198
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700199 struct vhost_dev dev;
Asias He3ab2e422013-04-27 11:16:48 +0800200 struct vhost_scsi_virtqueue vqs[VHOST_SCSI_MAX_VQ];
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700201
202 struct vhost_work vs_completion_work; /* cmd completion work item */
Asias He9d6064a2013-01-06 14:36:13 +0800203 struct llist_head vs_completion_list; /* cmd completion queue */
Asias Hea6c9af82013-04-25 15:35:21 +0800204
205 struct vhost_work vs_event_work; /* evt injection work item */
206 struct llist_head vs_event_list; /* evt injection queue */
207
208 bool vs_events_missed; /* any missed events, protected by vq->mutex */
209 int vs_events_nr; /* num of pending events, protected by vq->mutex */
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700210};
211
212/* Local pointer to allocated TCM configfs fabric module */
213static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
214
215static struct workqueue_struct *tcm_vhost_workqueue;
216
217/* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
218static DEFINE_MUTEX(tcm_vhost_mutex);
219static LIST_HEAD(tcm_vhost_list);
220
Asias He765b34f2013-01-22 11:20:25 +0800221static int iov_num_pages(struct iovec *iov)
222{
223 return (PAGE_ALIGN((unsigned long)iov->iov_base + iov->iov_len) -
224 ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
225}
226
Asias He0a1febf2013-06-05 21:17:38 +0800227static void tcm_vhost_done_inflight(struct kref *kref)
Asias Hef2f0173d2013-04-27 11:16:49 +0800228{
229 struct vhost_scsi_inflight *inflight;
230
231 inflight = container_of(kref, struct vhost_scsi_inflight, kref);
232 complete(&inflight->comp);
233}
234
235static void tcm_vhost_init_inflight(struct vhost_scsi *vs,
236 struct vhost_scsi_inflight *old_inflight[])
237{
238 struct vhost_scsi_inflight *new_inflight;
239 struct vhost_virtqueue *vq;
240 int idx, i;
241
242 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
243 vq = &vs->vqs[i].vq;
244
245 mutex_lock(&vq->mutex);
246
247 /* store old infight */
248 idx = vs->vqs[i].inflight_idx;
249 if (old_inflight)
250 old_inflight[i] = &vs->vqs[i].inflights[idx];
251
252 /* setup new infight */
253 vs->vqs[i].inflight_idx = idx ^ 1;
254 new_inflight = &vs->vqs[i].inflights[idx ^ 1];
255 kref_init(&new_inflight->kref);
256 init_completion(&new_inflight->comp);
257
258 mutex_unlock(&vq->mutex);
259 }
260}
261
262static struct vhost_scsi_inflight *
263tcm_vhost_get_inflight(struct vhost_virtqueue *vq)
264{
265 struct vhost_scsi_inflight *inflight;
266 struct vhost_scsi_virtqueue *svq;
267
268 svq = container_of(vq, struct vhost_scsi_virtqueue, vq);
269 inflight = &svq->inflights[svq->inflight_idx];
270 kref_get(&inflight->kref);
271
272 return inflight;
273}
274
275static void tcm_vhost_put_inflight(struct vhost_scsi_inflight *inflight)
276{
277 kref_put(&inflight->kref, tcm_vhost_done_inflight);
278}
279
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700280static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
281{
282 return 1;
283}
284
285static int tcm_vhost_check_false(struct se_portal_group *se_tpg)
286{
287 return 0;
288}
289
290static char *tcm_vhost_get_fabric_name(void)
291{
292 return "vhost";
293}
294
295static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg)
296{
297 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
298 struct tcm_vhost_tpg, se_tpg);
299 struct tcm_vhost_tport *tport = tpg->tport;
300
301 switch (tport->tport_proto_id) {
302 case SCSI_PROTOCOL_SAS:
303 return sas_get_fabric_proto_ident(se_tpg);
304 case SCSI_PROTOCOL_FCP:
305 return fc_get_fabric_proto_ident(se_tpg);
306 case SCSI_PROTOCOL_ISCSI:
307 return iscsi_get_fabric_proto_ident(se_tpg);
308 default:
309 pr_err("Unknown tport_proto_id: 0x%02x, using"
310 " SAS emulation\n", tport->tport_proto_id);
311 break;
312 }
313
314 return sas_get_fabric_proto_ident(se_tpg);
315}
316
317static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg)
318{
319 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
320 struct tcm_vhost_tpg, se_tpg);
321 struct tcm_vhost_tport *tport = tpg->tport;
322
323 return &tport->tport_name[0];
324}
325
326static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg)
327{
328 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
329 struct tcm_vhost_tpg, se_tpg);
330 return tpg->tport_tpgt;
331}
332
333static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
334{
335 return 1;
336}
337
Asias He683bd962013-05-06 16:38:27 +0800338static u32
339tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
340 struct se_node_acl *se_nacl,
341 struct t10_pr_registration *pr_reg,
342 int *format_code,
343 unsigned char *buf)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700344{
345 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
346 struct tcm_vhost_tpg, se_tpg);
347 struct tcm_vhost_tport *tport = tpg->tport;
348
349 switch (tport->tport_proto_id) {
350 case SCSI_PROTOCOL_SAS:
351 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
352 format_code, buf);
353 case SCSI_PROTOCOL_FCP:
354 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
355 format_code, buf);
356 case SCSI_PROTOCOL_ISCSI:
357 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
358 format_code, buf);
359 default:
360 pr_err("Unknown tport_proto_id: 0x%02x, using"
361 " SAS emulation\n", tport->tport_proto_id);
362 break;
363 }
364
365 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
366 format_code, buf);
367}
368
Asias He683bd962013-05-06 16:38:27 +0800369static u32
370tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
371 struct se_node_acl *se_nacl,
372 struct t10_pr_registration *pr_reg,
373 int *format_code)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700374{
375 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
376 struct tcm_vhost_tpg, se_tpg);
377 struct tcm_vhost_tport *tport = tpg->tport;
378
379 switch (tport->tport_proto_id) {
380 case SCSI_PROTOCOL_SAS:
381 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
382 format_code);
383 case SCSI_PROTOCOL_FCP:
384 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
385 format_code);
386 case SCSI_PROTOCOL_ISCSI:
387 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
388 format_code);
389 default:
390 pr_err("Unknown tport_proto_id: 0x%02x, using"
391 " SAS emulation\n", tport->tport_proto_id);
392 break;
393 }
394
395 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
396 format_code);
397}
398
Asias He683bd962013-05-06 16:38:27 +0800399static char *
400tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
401 const char *buf,
402 u32 *out_tid_len,
403 char **port_nexus_ptr)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700404{
405 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
406 struct tcm_vhost_tpg, se_tpg);
407 struct tcm_vhost_tport *tport = tpg->tport;
408
409 switch (tport->tport_proto_id) {
410 case SCSI_PROTOCOL_SAS:
411 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
412 port_nexus_ptr);
413 case SCSI_PROTOCOL_FCP:
414 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
415 port_nexus_ptr);
416 case SCSI_PROTOCOL_ISCSI:
417 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
418 port_nexus_ptr);
419 default:
420 pr_err("Unknown tport_proto_id: 0x%02x, using"
421 " SAS emulation\n", tport->tport_proto_id);
422 break;
423 }
424
425 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
426 port_nexus_ptr);
427}
428
Asias He683bd962013-05-06 16:38:27 +0800429static struct se_node_acl *
430tcm_vhost_alloc_fabric_acl(struct se_portal_group *se_tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700431{
432 struct tcm_vhost_nacl *nacl;
433
434 nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL);
435 if (!nacl) {
Masanari Iida744627e92012-11-05 23:30:40 +0900436 pr_err("Unable to allocate struct tcm_vhost_nacl\n");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700437 return NULL;
438 }
439
440 return &nacl->se_node_acl;
441}
442
Asias He683bd962013-05-06 16:38:27 +0800443static void
444tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
445 struct se_node_acl *se_nacl)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700446{
447 struct tcm_vhost_nacl *nacl = container_of(se_nacl,
448 struct tcm_vhost_nacl, se_node_acl);
449 kfree(nacl);
450}
451
452static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg)
453{
454 return 1;
455}
456
457static void tcm_vhost_release_cmd(struct se_cmd *se_cmd)
458{
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700459 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
460 struct tcm_vhost_cmd, tvc_se_cmd);
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700461 struct se_session *se_sess = se_cmd->se_sess;
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800462 int i;
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700463
464 if (tv_cmd->tvc_sgl_count) {
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700465 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
466 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
Michael S. Tsirkind3d665a2013-09-17 22:54:31 +0300467 }
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800468 if (tv_cmd->tvc_prot_sgl_count) {
469 for (i = 0; i < tv_cmd->tvc_prot_sgl_count; i++)
470 put_page(sg_page(&tv_cmd->tvc_prot_sgl[i]));
471 }
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700472
473 tcm_vhost_put_inflight(tv_cmd->inflight);
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700474 percpu_ida_free(&se_sess->sess_tag_pool, se_cmd->map_tag);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700475}
476
477static int tcm_vhost_shutdown_session(struct se_session *se_sess)
478{
479 return 0;
480}
481
482static void tcm_vhost_close_session(struct se_session *se_sess)
483{
484 return;
485}
486
487static u32 tcm_vhost_sess_get_index(struct se_session *se_sess)
488{
489 return 0;
490}
491
492static int tcm_vhost_write_pending(struct se_cmd *se_cmd)
493{
494 /* Go ahead and process the write immediately */
495 target_execute_cmd(se_cmd);
496 return 0;
497}
498
499static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd)
500{
501 return 0;
502}
503
504static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl)
505{
506 return;
507}
508
509static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd)
510{
511 return 0;
512}
513
514static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
515{
516 return 0;
517}
518
Asias He3c63f662013-05-06 16:38:29 +0800519static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *cmd)
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700520{
Asias He3c63f662013-05-06 16:38:29 +0800521 struct vhost_scsi *vs = cmd->tvc_vhost;
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700522
Asias He3c63f662013-05-06 16:38:29 +0800523 llist_add(&cmd->tvc_completion_list, &vs->vs_completion_list);
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700524
525 vhost_work_queue(&vs->dev, &vs->vs_completion_work);
526}
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700527
528static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
529{
Asias He3c63f662013-05-06 16:38:29 +0800530 struct tcm_vhost_cmd *cmd = container_of(se_cmd,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700531 struct tcm_vhost_cmd, tvc_se_cmd);
Asias He3c63f662013-05-06 16:38:29 +0800532 vhost_scsi_complete_cmd(cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700533 return 0;
534}
535
536static int tcm_vhost_queue_status(struct se_cmd *se_cmd)
537{
Asias He3c63f662013-05-06 16:38:29 +0800538 struct tcm_vhost_cmd *cmd = container_of(se_cmd,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700539 struct tcm_vhost_cmd, tvc_se_cmd);
Asias He3c63f662013-05-06 16:38:29 +0800540 vhost_scsi_complete_cmd(cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700541 return 0;
542}
543
Joern Engelb79fafa2013-07-03 11:22:17 -0400544static void tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700545{
Joern Engelb79fafa2013-07-03 11:22:17 -0400546 return;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700547}
548
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700549static void tcm_vhost_aborted_task(struct se_cmd *se_cmd)
550{
551 return;
552}
553
Asias Hea6c9af82013-04-25 15:35:21 +0800554static void tcm_vhost_free_evt(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
555{
556 vs->vs_events_nr--;
557 kfree(evt);
558}
559
Asias He683bd962013-05-06 16:38:27 +0800560static struct tcm_vhost_evt *
561tcm_vhost_allocate_evt(struct vhost_scsi *vs,
562 u32 event, u32 reason)
Asias Hea6c9af82013-04-25 15:35:21 +0800563{
Asias He3ab2e422013-04-27 11:16:48 +0800564 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Asias Hea6c9af82013-04-25 15:35:21 +0800565 struct tcm_vhost_evt *evt;
566
567 if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) {
568 vs->vs_events_missed = true;
569 return NULL;
570 }
571
572 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
573 if (!evt) {
574 vq_err(vq, "Failed to allocate tcm_vhost_evt\n");
575 vs->vs_events_missed = true;
576 return NULL;
577 }
578
579 evt->event.event = event;
580 evt->event.reason = reason;
581 vs->vs_events_nr++;
582
583 return evt;
584}
585
Asias He3c63f662013-05-06 16:38:29 +0800586static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700587{
Asias He3c63f662013-05-06 16:38:29 +0800588 struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700589
590 /* TODO locking against target/backend threads? */
Nicholas Bellinger6c131d02013-06-06 01:44:48 -0700591 transport_generic_free_cmd(se_cmd, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700592
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700593}
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700594
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700595static int vhost_scsi_check_stop_free(struct se_cmd *se_cmd)
596{
597 return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700598}
599
Asias He683bd962013-05-06 16:38:27 +0800600static void
601tcm_vhost_do_evt_work(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
Asias Hea6c9af82013-04-25 15:35:21 +0800602{
Asias He3ab2e422013-04-27 11:16:48 +0800603 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Asias Hea6c9af82013-04-25 15:35:21 +0800604 struct virtio_scsi_event *event = &evt->event;
605 struct virtio_scsi_event __user *eventp;
606 unsigned out, in;
607 int head, ret;
608
609 if (!vq->private_data) {
610 vs->vs_events_missed = true;
611 return;
612 }
613
614again:
615 vhost_disable_notify(&vs->dev, vq);
616 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
617 ARRAY_SIZE(vq->iov), &out, &in,
618 NULL, NULL);
619 if (head < 0) {
620 vs->vs_events_missed = true;
621 return;
622 }
623 if (head == vq->num) {
624 if (vhost_enable_notify(&vs->dev, vq))
625 goto again;
626 vs->vs_events_missed = true;
627 return;
628 }
629
630 if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) {
631 vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n",
632 vq->iov[out].iov_len);
633 vs->vs_events_missed = true;
634 return;
635 }
636
637 if (vs->vs_events_missed) {
638 event->event |= VIRTIO_SCSI_T_EVENTS_MISSED;
639 vs->vs_events_missed = false;
640 }
641
642 eventp = vq->iov[out].iov_base;
643 ret = __copy_to_user(eventp, event, sizeof(*event));
644 if (!ret)
645 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
646 else
647 vq_err(vq, "Faulted on tcm_vhost_send_event\n");
648}
649
650static void tcm_vhost_evt_work(struct vhost_work *work)
651{
652 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
653 vs_event_work);
Asias He3ab2e422013-04-27 11:16:48 +0800654 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Asias Hea6c9af82013-04-25 15:35:21 +0800655 struct tcm_vhost_evt *evt;
656 struct llist_node *llnode;
657
658 mutex_lock(&vq->mutex);
659 llnode = llist_del_all(&vs->vs_event_list);
660 while (llnode) {
661 evt = llist_entry(llnode, struct tcm_vhost_evt, list);
662 llnode = llist_next(llnode);
663 tcm_vhost_do_evt_work(vs, evt);
664 tcm_vhost_free_evt(vs, evt);
665 }
666 mutex_unlock(&vq->mutex);
667}
668
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700669/* Fill in status and signal that we are done processing this command
670 *
671 * This is scheduled in the vhost work queue so we are called with the owner
672 * process mm and can access the vring.
673 */
674static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
675{
676 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
677 vs_completion_work);
Asias He1b7f3902013-02-06 13:20:59 +0800678 DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
Asias He9d6064a2013-01-06 14:36:13 +0800679 struct virtio_scsi_cmd_resp v_rsp;
Asias He3c63f662013-05-06 16:38:29 +0800680 struct tcm_vhost_cmd *cmd;
Asias He9d6064a2013-01-06 14:36:13 +0800681 struct llist_node *llnode;
682 struct se_cmd *se_cmd;
Asias He1b7f3902013-02-06 13:20:59 +0800683 int ret, vq;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700684
Asias He1b7f3902013-02-06 13:20:59 +0800685 bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
Asias He9d6064a2013-01-06 14:36:13 +0800686 llnode = llist_del_all(&vs->vs_completion_list);
687 while (llnode) {
Asias He3c63f662013-05-06 16:38:29 +0800688 cmd = llist_entry(llnode, struct tcm_vhost_cmd,
Asias He9d6064a2013-01-06 14:36:13 +0800689 tvc_completion_list);
690 llnode = llist_next(llnode);
Asias He3c63f662013-05-06 16:38:29 +0800691 se_cmd = &cmd->tvc_se_cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700692
693 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
Asias He3c63f662013-05-06 16:38:29 +0800694 cmd, se_cmd->residual_count, se_cmd->scsi_status);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700695
696 memset(&v_rsp, 0, sizeof(v_rsp));
697 v_rsp.resid = se_cmd->residual_count;
698 /* TODO is status_qualifier field needed? */
699 v_rsp.status = se_cmd->scsi_status;
700 v_rsp.sense_len = se_cmd->scsi_sense_length;
Asias He3c63f662013-05-06 16:38:29 +0800701 memcpy(v_rsp.sense, cmd->tvc_sense_buf,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700702 v_rsp.sense_len);
Asias He3c63f662013-05-06 16:38:29 +0800703 ret = copy_to_user(cmd->tvc_resp, &v_rsp, sizeof(v_rsp));
Asias He1b7f3902013-02-06 13:20:59 +0800704 if (likely(ret == 0)) {
Asias He3ab2e422013-04-27 11:16:48 +0800705 struct vhost_scsi_virtqueue *q;
Asias He3c63f662013-05-06 16:38:29 +0800706 vhost_add_used(cmd->tvc_vq, cmd->tvc_vq_desc, 0);
707 q = container_of(cmd->tvc_vq, struct vhost_scsi_virtqueue, vq);
Asias He3ab2e422013-04-27 11:16:48 +0800708 vq = q - vs->vqs;
Asias He1b7f3902013-02-06 13:20:59 +0800709 __set_bit(vq, signal);
710 } else
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700711 pr_err("Faulted on virtio_scsi_cmd_resp\n");
712
Asias He3c63f662013-05-06 16:38:29 +0800713 vhost_scsi_free_cmd(cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700714 }
715
Asias He1b7f3902013-02-06 13:20:59 +0800716 vq = -1;
717 while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1))
718 < VHOST_SCSI_MAX_VQ)
Asias He3ab2e422013-04-27 11:16:48 +0800719 vhost_signal(&vs->dev, &vs->vqs[vq].vq);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700720}
721
Asias He683bd962013-05-06 16:38:27 +0800722static struct tcm_vhost_cmd *
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700723vhost_scsi_get_tag(struct vhost_virtqueue *vq,
Asias He98718312013-05-06 16:38:28 +0800724 struct tcm_vhost_tpg *tpg,
Asias He683bd962013-05-06 16:38:27 +0800725 struct virtio_scsi_cmd_req *v_req,
726 u32 exp_data_len,
727 int data_direction)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700728{
Asias He3c63f662013-05-06 16:38:29 +0800729 struct tcm_vhost_cmd *cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700730 struct tcm_vhost_nexus *tv_nexus;
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700731 struct se_session *se_sess;
Nicholas Bellingerb1935f62014-02-22 18:08:24 -0800732 struct scatterlist *sg, *prot_sg;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700733 struct page **pages;
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700734 int tag;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700735
Asias He98718312013-05-06 16:38:28 +0800736 tv_nexus = tpg->tpg_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700737 if (!tv_nexus) {
738 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
739 return ERR_PTR(-EIO);
740 }
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700741 se_sess = tv_nexus->tvn_se_sess;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700742
Kent Overstreet6f6b5d12014-01-19 08:26:37 +0000743 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
Nicholas Bellinger4a47d3a2013-09-23 11:42:28 -0700744 if (tag < 0) {
745 pr_err("Unable to obtain tag for tcm_vhost_cmd\n");
746 return ERR_PTR(-ENOMEM);
747 }
748
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700749 cmd = &((struct tcm_vhost_cmd *)se_sess->sess_cmd_map)[tag];
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700750 sg = cmd->tvc_sgl;
Nicholas Bellingerb1935f62014-02-22 18:08:24 -0800751 prot_sg = cmd->tvc_prot_sgl;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700752 pages = cmd->tvc_upages;
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700753 memset(cmd, 0, sizeof(struct tcm_vhost_cmd));
754
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700755 cmd->tvc_sgl = sg;
Nicholas Bellingerb1935f62014-02-22 18:08:24 -0800756 cmd->tvc_prot_sgl = prot_sg;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700757 cmd->tvc_upages = pages;
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700758 cmd->tvc_se_cmd.map_tag = tag;
Asias He3c63f662013-05-06 16:38:29 +0800759 cmd->tvc_tag = v_req->tag;
760 cmd->tvc_task_attr = v_req->task_attr;
761 cmd->tvc_exp_data_len = exp_data_len;
762 cmd->tvc_data_direction = data_direction;
763 cmd->tvc_nexus = tv_nexus;
764 cmd->inflight = tcm_vhost_get_inflight(vq);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700765
Asias He3c63f662013-05-06 16:38:29 +0800766 return cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700767}
768
769/*
770 * Map a user memory range into a scatterlist
771 *
772 * Returns the number of scatterlist entries used or -errno on error.
773 */
Asias He683bd962013-05-06 16:38:27 +0800774static int
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700775vhost_scsi_map_to_sgl(struct tcm_vhost_cmd *tv_cmd,
776 struct scatterlist *sgl,
Asias He683bd962013-05-06 16:38:27 +0800777 unsigned int sgl_count,
778 struct iovec *iov,
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800779 struct page **pages,
780 bool write)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700781{
Asias He18100532013-01-22 11:20:27 +0800782 unsigned int npages = 0, pages_nr, offset, nbytes;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700783 struct scatterlist *sg = sgl;
Asias He18100532013-01-22 11:20:27 +0800784 void __user *ptr = iov->iov_base;
785 size_t len = iov->iov_len;
Asias He18100532013-01-22 11:20:27 +0800786 int ret, i;
787
788 pages_nr = iov_num_pages(iov);
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800789 if (pages_nr > sgl_count) {
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700790 pr_err("vhost_scsi_map_to_sgl() pages_nr: %u greater than"
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800791 " sgl_count: %u\n", pages_nr, sgl_count);
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700792 return -ENOBUFS;
793 }
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800794 if (pages_nr > TCM_VHOST_PREALLOC_UPAGES) {
795 pr_err("vhost_scsi_map_to_sgl() pages_nr: %u greater than"
796 " preallocated TCM_VHOST_PREALLOC_UPAGES: %u\n",
797 pages_nr, TCM_VHOST_PREALLOC_UPAGES);
798 return -ENOBUFS;
799 }
Asias He18100532013-01-22 11:20:27 +0800800
801 ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages);
802 /* No pages were pinned */
803 if (ret < 0)
804 goto out;
805 /* Less pages pinned than wanted */
806 if (ret != pages_nr) {
807 for (i = 0; i < ret; i++)
808 put_page(pages[i]);
809 ret = -EFAULT;
810 goto out;
811 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700812
813 while (len > 0) {
Asias He18100532013-01-22 11:20:27 +0800814 offset = (uintptr_t)ptr & ~PAGE_MASK;
815 nbytes = min_t(unsigned int, PAGE_SIZE - offset, len);
816 sg_set_page(sg, pages[npages], nbytes, offset);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700817 ptr += nbytes;
818 len -= nbytes;
819 sg++;
820 npages++;
821 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700822
Asias He18100532013-01-22 11:20:27 +0800823out:
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700824 return ret;
825}
826
Asias He683bd962013-05-06 16:38:27 +0800827static int
Asias He3c63f662013-05-06 16:38:29 +0800828vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *cmd,
Asias He683bd962013-05-06 16:38:27 +0800829 struct iovec *iov,
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800830 int niov,
831 bool write)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700832{
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800833 struct scatterlist *sg = cmd->tvc_sgl;
834 unsigned int sgl_count = 0;
835 int ret, i;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700836
Asias Hef3158f32013-01-22 11:20:26 +0800837 for (i = 0; i < niov; i++)
838 sgl_count += iov_num_pages(&iov[i]);
839
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800840 if (sgl_count > TCM_VHOST_PREALLOC_SGLS) {
841 pr_err("vhost_scsi_map_iov_to_sgl() sgl_count: %u greater than"
842 " preallocated TCM_VHOST_PREALLOC_SGLS: %u\n",
843 sgl_count, TCM_VHOST_PREALLOC_SGLS);
844 return -ENOBUFS;
845 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700846
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700847 pr_debug("%s sg %p sgl_count %u\n", __func__, sg, sgl_count);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700848 sg_init_table(sg, sgl_count);
Asias He3c63f662013-05-06 16:38:29 +0800849 cmd->tvc_sgl_count = sgl_count;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700850
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800851 pr_debug("Mapping iovec %p for %u pages\n", &iov[0], sgl_count);
852
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700853 for (i = 0; i < niov; i++) {
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700854 ret = vhost_scsi_map_to_sgl(cmd, sg, sgl_count, &iov[i],
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800855 cmd->tvc_upages, write);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700856 if (ret < 0) {
Asias He3c63f662013-05-06 16:38:29 +0800857 for (i = 0; i < cmd->tvc_sgl_count; i++)
858 put_page(sg_page(&cmd->tvc_sgl[i]));
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700859
Asias He3c63f662013-05-06 16:38:29 +0800860 cmd->tvc_sgl_count = 0;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700861 return ret;
862 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700863 sg += ret;
864 sgl_count -= ret;
865 }
866 return 0;
867}
868
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800869static int
870vhost_scsi_map_iov_to_prot(struct tcm_vhost_cmd *cmd,
871 struct iovec *iov,
872 int niov,
873 bool write)
874{
875 struct scatterlist *prot_sg = cmd->tvc_prot_sgl;
876 unsigned int prot_sgl_count = 0;
877 int ret, i;
878
879 for (i = 0; i < niov; i++)
880 prot_sgl_count += iov_num_pages(&iov[i]);
881
882 if (prot_sgl_count > TCM_VHOST_PREALLOC_PROT_SGLS) {
883 pr_err("vhost_scsi_map_iov_to_prot() sgl_count: %u greater than"
884 " preallocated TCM_VHOST_PREALLOC_PROT_SGLS: %u\n",
885 prot_sgl_count, TCM_VHOST_PREALLOC_PROT_SGLS);
886 return -ENOBUFS;
887 }
888
889 pr_debug("%s prot_sg %p prot_sgl_count %u\n", __func__,
890 prot_sg, prot_sgl_count);
891 sg_init_table(prot_sg, prot_sgl_count);
892 cmd->tvc_prot_sgl_count = prot_sgl_count;
893
894 for (i = 0; i < niov; i++) {
895 ret = vhost_scsi_map_to_sgl(cmd, prot_sg, prot_sgl_count, &iov[i],
896 cmd->tvc_upages, write);
897 if (ret < 0) {
898 for (i = 0; i < cmd->tvc_prot_sgl_count; i++)
899 put_page(sg_page(&cmd->tvc_prot_sgl[i]));
900
901 cmd->tvc_prot_sgl_count = 0;
902 return ret;
903 }
904 prot_sg += ret;
905 prot_sgl_count -= ret;
906 }
907 return 0;
908}
909
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700910static void tcm_vhost_submission_work(struct work_struct *work)
911{
Asias He3c63f662013-05-06 16:38:29 +0800912 struct tcm_vhost_cmd *cmd =
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700913 container_of(work, struct tcm_vhost_cmd, work);
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700914 struct tcm_vhost_nexus *tv_nexus;
Asias He3c63f662013-05-06 16:38:29 +0800915 struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700916 struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL;
917 int rc, sg_no_bidi = 0;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700918
Asias He3c63f662013-05-06 16:38:29 +0800919 if (cmd->tvc_sgl_count) {
920 sg_ptr = cmd->tvc_sgl;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700921/* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
922#if 0
923 if (se_cmd->se_cmd_flags & SCF_BIDI) {
924 sg_bidi_ptr = NULL;
925 sg_no_bidi = 0;
926 }
927#endif
928 } else {
929 sg_ptr = NULL;
930 }
Asias He3c63f662013-05-06 16:38:29 +0800931 tv_nexus = cmd->tvc_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700932
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700933 rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
Asias He3c63f662013-05-06 16:38:29 +0800934 cmd->tvc_cdb, &cmd->tvc_sense_buf[0],
935 cmd->tvc_lun, cmd->tvc_exp_data_len,
936 cmd->tvc_task_attr, cmd->tvc_data_direction,
Linus Torvalds6d2fa9e2013-07-11 12:57:19 -0700937 TARGET_SCF_ACK_KREF, sg_ptr, cmd->tvc_sgl_count,
Nicholas Bellingerdef2b332013-12-23 20:38:30 +0000938 sg_bidi_ptr, sg_no_bidi, NULL, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700939 if (rc < 0) {
940 transport_send_check_condition_and_sense(se_cmd,
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700941 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700942 transport_generic_free_cmd(se_cmd, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700943 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700944}
945
Asias He683bd962013-05-06 16:38:27 +0800946static void
947vhost_scsi_send_bad_target(struct vhost_scsi *vs,
948 struct vhost_virtqueue *vq,
949 int head, unsigned out)
Asias He637ab212013-04-10 15:06:15 +0800950{
951 struct virtio_scsi_cmd_resp __user *resp;
952 struct virtio_scsi_cmd_resp rsp;
953 int ret;
954
955 memset(&rsp, 0, sizeof(rsp));
956 rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
957 resp = vq->iov[out].iov_base;
958 ret = __copy_to_user(resp, &rsp, sizeof(rsp));
959 if (!ret)
960 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
961 else
962 pr_err("Faulted on virtio_scsi_cmd_resp\n");
963}
964
Asias He683bd962013-05-06 16:38:27 +0800965static void
966vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700967{
Asias He4f7f46d2013-04-03 14:17:37 +0800968 struct tcm_vhost_tpg **vs_tpg;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700969 struct virtio_scsi_cmd_req v_req;
Asias He98718312013-05-06 16:38:28 +0800970 struct tcm_vhost_tpg *tpg;
Asias He3c63f662013-05-06 16:38:29 +0800971 struct tcm_vhost_cmd *cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700972 u32 exp_data_len, data_first, data_num, data_direction;
973 unsigned out, in, i;
974 int head, ret;
Asias He67e18cf2013-02-05 12:31:57 +0800975 u8 target;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700976
Asias Hee7802212013-05-07 14:54:35 +0800977 mutex_lock(&vq->mutex);
Asias He4f7f46d2013-04-03 14:17:37 +0800978 /*
979 * We can handle the vq only after the endpoint is setup by calling the
980 * VHOST_SCSI_SET_ENDPOINT ioctl.
Asias He4f7f46d2013-04-03 14:17:37 +0800981 */
Asias Hee7802212013-05-07 14:54:35 +0800982 vs_tpg = vq->private_data;
Asias He4f7f46d2013-04-03 14:17:37 +0800983 if (!vs_tpg)
Asias Hee7802212013-05-07 14:54:35 +0800984 goto out;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700985
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700986 vhost_disable_notify(&vs->dev, vq);
987
988 for (;;) {
989 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
990 ARRAY_SIZE(vq->iov), &out, &in,
991 NULL, NULL);
992 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
993 head, out, in);
994 /* On error, stop handling until the next kick. */
995 if (unlikely(head < 0))
996 break;
997 /* Nothing new? Wait for eventfd to tell us they refilled. */
998 if (head == vq->num) {
999 if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
1000 vhost_disable_notify(&vs->dev, vq);
1001 continue;
1002 }
1003 break;
1004 }
1005
1006/* FIXME: BIDI operation */
1007 if (out == 1 && in == 1) {
1008 data_direction = DMA_NONE;
1009 data_first = 0;
1010 data_num = 0;
1011 } else if (out == 1 && in > 1) {
1012 data_direction = DMA_FROM_DEVICE;
1013 data_first = out + 1;
1014 data_num = in - 1;
1015 } else if (out > 1 && in == 1) {
1016 data_direction = DMA_TO_DEVICE;
1017 data_first = 1;
1018 data_num = out - 1;
1019 } else {
1020 vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
1021 out, in);
1022 break;
1023 }
1024
1025 /*
1026 * Check for a sane resp buffer so we can report errors to
1027 * the guest.
1028 */
1029 if (unlikely(vq->iov[out].iov_len !=
1030 sizeof(struct virtio_scsi_cmd_resp))) {
1031 vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
1032 " bytes\n", vq->iov[out].iov_len);
1033 break;
1034 }
1035
1036 if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) {
1037 vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu"
1038 " bytes\n", vq->iov[0].iov_len);
1039 break;
1040 }
1041 pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
1042 " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req));
1043 ret = __copy_from_user(&v_req, vq->iov[0].iov_base,
1044 sizeof(v_req));
1045 if (unlikely(ret)) {
1046 vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
1047 break;
1048 }
1049
Venkatesh Srinivas7fe412d2014-02-24 14:13:32 -08001050 /* virtio-scsi spec requires byte 0 of the lun to be 1 */
1051 if (unlikely(v_req.lun[0] != 1)) {
1052 vhost_scsi_send_bad_target(vs, vq, head, out);
1053 continue;
1054 }
1055
Asias He67e18cf2013-02-05 12:31:57 +08001056 /* Extract the tpgt */
1057 target = v_req.lun[1];
Asias He98718312013-05-06 16:38:28 +08001058 tpg = ACCESS_ONCE(vs_tpg[target]);
Asias He67e18cf2013-02-05 12:31:57 +08001059
1060 /* Target does not exist, fail the request */
Asias He98718312013-05-06 16:38:28 +08001061 if (unlikely(!tpg)) {
Asias He637ab212013-04-10 15:06:15 +08001062 vhost_scsi_send_bad_target(vs, vq, head, out);
Asias He67e18cf2013-02-05 12:31:57 +08001063 continue;
1064 }
1065
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001066 exp_data_len = 0;
1067 for (i = 0; i < data_num; i++)
1068 exp_data_len += vq->iov[data_first + i].iov_len;
1069
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -07001070 cmd = vhost_scsi_get_tag(vq, tpg, &v_req,
1071 exp_data_len, data_direction);
Asias He3c63f662013-05-06 16:38:29 +08001072 if (IS_ERR(cmd)) {
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -07001073 vq_err(vq, "vhost_scsi_get_tag failed %ld\n",
Asias He3c63f662013-05-06 16:38:29 +08001074 PTR_ERR(cmd));
Asias He055f6482013-04-10 15:06:16 +08001075 goto err_cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001076 }
1077 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
Asias He3c63f662013-05-06 16:38:29 +08001078 ": %d\n", cmd, exp_data_len, data_direction);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001079
Asias He3c63f662013-05-06 16:38:29 +08001080 cmd->tvc_vhost = vs;
1081 cmd->tvc_vq = vq;
1082 cmd->tvc_resp = vq->iov[out].iov_base;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001083
1084 /*
Asias He3c63f662013-05-06 16:38:29 +08001085 * Copy in the recieved CDB descriptor into cmd->tvc_cdb
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001086 * that will be used by tcm_vhost_new_cmd_map() and down into
1087 * target_setup_cmd_from_cdb()
1088 */
Asias He3c63f662013-05-06 16:38:29 +08001089 memcpy(cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001090 /*
1091 * Check that the recieved CDB size does not exceeded our
1092 * hardcoded max for tcm_vhost
1093 */
1094 /* TODO what if cdb was too small for varlen cdb header? */
Asias He3c63f662013-05-06 16:38:29 +08001095 if (unlikely(scsi_command_size(cmd->tvc_cdb) >
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001096 TCM_VHOST_MAX_CDB_SIZE)) {
1097 vq_err(vq, "Received SCSI CDB with command_size: %d that"
1098 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
Asias He3c63f662013-05-06 16:38:29 +08001099 scsi_command_size(cmd->tvc_cdb),
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001100 TCM_VHOST_MAX_CDB_SIZE);
Asias He055f6482013-04-10 15:06:16 +08001101 goto err_free;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001102 }
Asias He3c63f662013-05-06 16:38:29 +08001103 cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001104
1105 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
Asias He3c63f662013-05-06 16:38:29 +08001106 cmd->tvc_cdb[0], cmd->tvc_lun);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001107
1108 if (data_direction != DMA_NONE) {
Asias He3c63f662013-05-06 16:38:29 +08001109 ret = vhost_scsi_map_iov_to_sgl(cmd,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001110 &vq->iov[data_first], data_num,
Nicholas Bellinger60a01f52013-10-25 10:44:15 -07001111 data_direction == DMA_FROM_DEVICE);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001112 if (unlikely(ret)) {
1113 vq_err(vq, "Failed to map iov to sgl\n");
Asias He055f6482013-04-10 15:06:16 +08001114 goto err_free;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001115 }
1116 }
1117
1118 /*
1119 * Save the descriptor from vhost_get_vq_desc() to be used to
1120 * complete the virtio-scsi request in TCM callback context via
1121 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
1122 */
Asias He3c63f662013-05-06 16:38:29 +08001123 cmd->tvc_vq_desc = head;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001124 /*
1125 * Dispatch tv_cmd descriptor for cmwq execution in process
1126 * context provided by tcm_vhost_workqueue. This also ensures
1127 * tv_cmd is executed on the same kworker CPU as this vhost
1128 * thread to gain positive L2 cache locality effects..
1129 */
Asias He3c63f662013-05-06 16:38:29 +08001130 INIT_WORK(&cmd->work, tcm_vhost_submission_work);
1131 queue_work(tcm_vhost_workqueue, &cmd->work);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001132 }
1133
1134 mutex_unlock(&vq->mutex);
Asias He7ea206c2013-04-10 15:06:14 +08001135 return;
1136
Asias He055f6482013-04-10 15:06:16 +08001137err_free:
Asias He3c63f662013-05-06 16:38:29 +08001138 vhost_scsi_free_cmd(cmd);
Asias He055f6482013-04-10 15:06:16 +08001139err_cmd:
1140 vhost_scsi_send_bad_target(vs, vq, head, out);
Asias Hee7802212013-05-07 14:54:35 +08001141out:
Asias He7ea206c2013-04-10 15:06:14 +08001142 mutex_unlock(&vq->mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001143}
1144
1145static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
1146{
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001147 pr_debug("%s: The handling func for control queue.\n", __func__);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001148}
1149
Asias He683bd962013-05-06 16:38:27 +08001150static void
1151tcm_vhost_send_evt(struct vhost_scsi *vs,
1152 struct tcm_vhost_tpg *tpg,
1153 struct se_lun *lun,
1154 u32 event,
1155 u32 reason)
Asias Hea6c9af82013-04-25 15:35:21 +08001156{
1157 struct tcm_vhost_evt *evt;
1158
1159 evt = tcm_vhost_allocate_evt(vs, event, reason);
1160 if (!evt)
1161 return;
1162
1163 if (tpg && lun) {
1164 /* TODO: share lun setup code with virtio-scsi.ko */
1165 /*
1166 * Note: evt->event is zeroed when we allocate it and
1167 * lun[4-7] need to be zero according to virtio-scsi spec.
1168 */
1169 evt->event.lun[0] = 0x01;
1170 evt->event.lun[1] = tpg->tport_tpgt & 0xFF;
1171 if (lun->unpacked_lun >= 256)
1172 evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
1173 evt->event.lun[3] = lun->unpacked_lun & 0xFF;
1174 }
1175
1176 llist_add(&evt->list, &vs->vs_event_list);
1177 vhost_work_queue(&vs->dev, &vs->vs_event_work);
1178}
1179
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001180static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
1181{
Asias Hea6c9af82013-04-25 15:35:21 +08001182 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1183 poll.work);
1184 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1185
1186 mutex_lock(&vq->mutex);
1187 if (!vq->private_data)
1188 goto out;
1189
1190 if (vs->vs_events_missed)
1191 tcm_vhost_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
1192out:
1193 mutex_unlock(&vq->mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001194}
1195
1196static void vhost_scsi_handle_kick(struct vhost_work *work)
1197{
1198 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1199 poll.work);
1200 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1201
Asias He1b7f3902013-02-06 13:20:59 +08001202 vhost_scsi_handle_vq(vs, vq);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001203}
1204
Asias He4f7f46d2013-04-03 14:17:37 +08001205static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
1206{
Asias He3ab2e422013-04-27 11:16:48 +08001207 vhost_poll_flush(&vs->vqs[index].vq.poll);
Asias He4f7f46d2013-04-03 14:17:37 +08001208}
1209
Michael S. Tsirkin3dfbff32013-04-28 15:38:52 +03001210/* Callers must hold dev mutex */
Asias He4f7f46d2013-04-03 14:17:37 +08001211static void vhost_scsi_flush(struct vhost_scsi *vs)
1212{
Asias Hef2f0173d2013-04-27 11:16:49 +08001213 struct vhost_scsi_inflight *old_inflight[VHOST_SCSI_MAX_VQ];
Asias He4f7f46d2013-04-03 14:17:37 +08001214 int i;
1215
Asias Hef2f0173d2013-04-27 11:16:49 +08001216 /* Init new inflight and remember the old inflight */
1217 tcm_vhost_init_inflight(vs, old_inflight);
1218
1219 /*
1220 * The inflight->kref was initialized to 1. We decrement it here to
1221 * indicate the start of the flush operation so that it will reach 0
1222 * when all the reqs are finished.
1223 */
1224 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1225 kref_put(&old_inflight[i]->kref, tcm_vhost_done_inflight);
1226
1227 /* Flush both the vhost poll and vhost work */
Asias He4f7f46d2013-04-03 14:17:37 +08001228 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1229 vhost_scsi_flush_vq(vs, i);
1230 vhost_work_flush(&vs->dev, &vs->vs_completion_work);
Asias Hea6c9af82013-04-25 15:35:21 +08001231 vhost_work_flush(&vs->dev, &vs->vs_event_work);
Asias Hef2f0173d2013-04-27 11:16:49 +08001232
1233 /* Wait for all reqs issued before the flush to be finished */
1234 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1235 wait_for_completion(&old_inflight[i]->comp);
Asias He4f7f46d2013-04-03 14:17:37 +08001236}
1237
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001238/*
1239 * Called from vhost_scsi_ioctl() context to walk the list of available
1240 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
Asias Hef2b7daf2013-04-25 15:35:20 +08001241 *
1242 * The lock nesting rule is:
1243 * tcm_vhost_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001244 */
Asias He683bd962013-05-06 16:38:27 +08001245static int
1246vhost_scsi_set_endpoint(struct vhost_scsi *vs,
1247 struct vhost_scsi_target *t)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001248{
1249 struct tcm_vhost_tport *tv_tport;
Asias He98718312013-05-06 16:38:28 +08001250 struct tcm_vhost_tpg *tpg;
Asias He4f7f46d2013-04-03 14:17:37 +08001251 struct tcm_vhost_tpg **vs_tpg;
1252 struct vhost_virtqueue *vq;
1253 int index, ret, i, len;
Asias He67e18cf2013-02-05 12:31:57 +08001254 bool match = false;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001255
Asias Hef2b7daf2013-04-25 15:35:20 +08001256 mutex_lock(&tcm_vhost_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001257 mutex_lock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001258
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001259 /* Verify that ring has been setup correctly. */
1260 for (index = 0; index < vs->dev.nvqs; ++index) {
1261 /* Verify that ring has been setup correctly. */
Asias He3ab2e422013-04-27 11:16:48 +08001262 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
Asias Hef2b7daf2013-04-25 15:35:20 +08001263 ret = -EFAULT;
1264 goto out;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001265 }
1266 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001267
Asias He4f7f46d2013-04-03 14:17:37 +08001268 len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
1269 vs_tpg = kzalloc(len, GFP_KERNEL);
1270 if (!vs_tpg) {
Asias Hef2b7daf2013-04-25 15:35:20 +08001271 ret = -ENOMEM;
1272 goto out;
Asias He4f7f46d2013-04-03 14:17:37 +08001273 }
1274 if (vs->vs_tpg)
1275 memcpy(vs_tpg, vs->vs_tpg, len);
1276
Asias He98718312013-05-06 16:38:28 +08001277 list_for_each_entry(tpg, &tcm_vhost_list, tv_tpg_list) {
1278 mutex_lock(&tpg->tv_tpg_mutex);
1279 if (!tpg->tpg_nexus) {
1280 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001281 continue;
1282 }
Asias He98718312013-05-06 16:38:28 +08001283 if (tpg->tv_tpg_vhost_count != 0) {
1284 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001285 continue;
1286 }
Asias He98718312013-05-06 16:38:28 +08001287 tv_tport = tpg->tport;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001288
Asias He67e18cf2013-02-05 12:31:57 +08001289 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
Asias He98718312013-05-06 16:38:28 +08001290 if (vs->vs_tpg && vs->vs_tpg[tpg->tport_tpgt]) {
Asias He4f7f46d2013-04-03 14:17:37 +08001291 kfree(vs_tpg);
Asias He98718312013-05-06 16:38:28 +08001292 mutex_unlock(&tpg->tv_tpg_mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001293 ret = -EEXIST;
1294 goto out;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001295 }
Asias He98718312013-05-06 16:38:28 +08001296 tpg->tv_tpg_vhost_count++;
1297 tpg->vhost_scsi = vs;
1298 vs_tpg[tpg->tport_tpgt] = tpg;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001299 smp_mb__after_atomic_inc();
Asias He67e18cf2013-02-05 12:31:57 +08001300 match = true;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001301 }
Asias He98718312013-05-06 16:38:28 +08001302 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001303 }
Asias He67e18cf2013-02-05 12:31:57 +08001304
1305 if (match) {
1306 memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
1307 sizeof(vs->vs_vhost_wwpn));
Asias He4f7f46d2013-04-03 14:17:37 +08001308 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
Asias He3ab2e422013-04-27 11:16:48 +08001309 vq = &vs->vqs[i].vq;
Asias He4f7f46d2013-04-03 14:17:37 +08001310 mutex_lock(&vq->mutex);
Asias He22fa90c2013-05-07 14:54:36 +08001311 vq->private_data = vs_tpg;
Asias Hedfd5d562013-04-03 14:17:38 +08001312 vhost_init_used(vq);
Asias He4f7f46d2013-04-03 14:17:37 +08001313 mutex_unlock(&vq->mutex);
1314 }
Asias He67e18cf2013-02-05 12:31:57 +08001315 ret = 0;
1316 } else {
1317 ret = -EEXIST;
1318 }
1319
Asias He4f7f46d2013-04-03 14:17:37 +08001320 /*
1321 * Act as synchronize_rcu to make sure access to
1322 * old vs->vs_tpg is finished.
1323 */
1324 vhost_scsi_flush(vs);
1325 kfree(vs->vs_tpg);
1326 vs->vs_tpg = vs_tpg;
1327
Asias Hef2b7daf2013-04-25 15:35:20 +08001328out:
Asias He67e18cf2013-02-05 12:31:57 +08001329 mutex_unlock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001330 mutex_unlock(&tcm_vhost_mutex);
Asias He67e18cf2013-02-05 12:31:57 +08001331 return ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001332}
1333
Asias He683bd962013-05-06 16:38:27 +08001334static int
1335vhost_scsi_clear_endpoint(struct vhost_scsi *vs,
1336 struct vhost_scsi_target *t)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001337{
1338 struct tcm_vhost_tport *tv_tport;
Asias He98718312013-05-06 16:38:28 +08001339 struct tcm_vhost_tpg *tpg;
Asias He4f7f46d2013-04-03 14:17:37 +08001340 struct vhost_virtqueue *vq;
1341 bool match = false;
Asias He67e18cf2013-02-05 12:31:57 +08001342 int index, ret, i;
1343 u8 target;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001344
Asias Hef2b7daf2013-04-25 15:35:20 +08001345 mutex_lock(&tcm_vhost_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001346 mutex_lock(&vs->dev.mutex);
1347 /* Verify that ring has been setup correctly. */
1348 for (index = 0; index < vs->dev.nvqs; ++index) {
Asias He3ab2e422013-04-27 11:16:48 +08001349 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001350 ret = -EFAULT;
Asias He038e0af2013-03-15 09:14:05 +08001351 goto err_dev;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001352 }
1353 }
Asias He4f7f46d2013-04-03 14:17:37 +08001354
1355 if (!vs->vs_tpg) {
Asias Hef2b7daf2013-04-25 15:35:20 +08001356 ret = 0;
1357 goto err_dev;
Asias He4f7f46d2013-04-03 14:17:37 +08001358 }
1359
Asias He67e18cf2013-02-05 12:31:57 +08001360 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
1361 target = i;
Asias He98718312013-05-06 16:38:28 +08001362 tpg = vs->vs_tpg[target];
1363 if (!tpg)
Asias He67e18cf2013-02-05 12:31:57 +08001364 continue;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001365
Asias He98718312013-05-06 16:38:28 +08001366 mutex_lock(&tpg->tv_tpg_mutex);
1367 tv_tport = tpg->tport;
Asias He67e18cf2013-02-05 12:31:57 +08001368 if (!tv_tport) {
1369 ret = -ENODEV;
Asias He038e0af2013-03-15 09:14:05 +08001370 goto err_tpg;
Asias He67e18cf2013-02-05 12:31:57 +08001371 }
1372
1373 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
Asias He98718312013-05-06 16:38:28 +08001374 pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu"
Asias He67e18cf2013-02-05 12:31:57 +08001375 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
Asias He98718312013-05-06 16:38:28 +08001376 tv_tport->tport_name, tpg->tport_tpgt,
Asias He67e18cf2013-02-05 12:31:57 +08001377 t->vhost_wwpn, t->vhost_tpgt);
1378 ret = -EINVAL;
Asias He038e0af2013-03-15 09:14:05 +08001379 goto err_tpg;
Asias He67e18cf2013-02-05 12:31:57 +08001380 }
Asias He98718312013-05-06 16:38:28 +08001381 tpg->tv_tpg_vhost_count--;
1382 tpg->vhost_scsi = NULL;
Asias He67e18cf2013-02-05 12:31:57 +08001383 vs->vs_tpg[target] = NULL;
Asias He4f7f46d2013-04-03 14:17:37 +08001384 match = true;
Asias He98718312013-05-06 16:38:28 +08001385 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001386 }
Asias He4f7f46d2013-04-03 14:17:37 +08001387 if (match) {
1388 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
Asias He3ab2e422013-04-27 11:16:48 +08001389 vq = &vs->vqs[i].vq;
Asias He4f7f46d2013-04-03 14:17:37 +08001390 mutex_lock(&vq->mutex);
Asias He22fa90c2013-05-07 14:54:36 +08001391 vq->private_data = NULL;
Asias He4f7f46d2013-04-03 14:17:37 +08001392 mutex_unlock(&vq->mutex);
1393 }
1394 }
1395 /*
1396 * Act as synchronize_rcu to make sure access to
1397 * old vs->vs_tpg is finished.
1398 */
1399 vhost_scsi_flush(vs);
1400 kfree(vs->vs_tpg);
1401 vs->vs_tpg = NULL;
Asias Hea6c9af82013-04-25 15:35:21 +08001402 WARN_ON(vs->vs_events_nr);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001403 mutex_unlock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001404 mutex_unlock(&tcm_vhost_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001405 return 0;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001406
Asias He038e0af2013-03-15 09:14:05 +08001407err_tpg:
Asias He98718312013-05-06 16:38:28 +08001408 mutex_unlock(&tpg->tv_tpg_mutex);
Asias He038e0af2013-03-15 09:14:05 +08001409err_dev:
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001410 mutex_unlock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001411 mutex_unlock(&tcm_vhost_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001412 return ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001413}
1414
Asias He4f7f46d2013-04-03 14:17:37 +08001415static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
1416{
1417 if (features & ~VHOST_SCSI_FEATURES)
1418 return -EOPNOTSUPP;
1419
1420 mutex_lock(&vs->dev.mutex);
1421 if ((features & (1 << VHOST_F_LOG_ALL)) &&
1422 !vhost_log_access_ok(&vs->dev)) {
1423 mutex_unlock(&vs->dev.mutex);
1424 return -EFAULT;
1425 }
1426 vs->dev.acked_features = features;
1427 smp_wmb();
1428 vhost_scsi_flush(vs);
1429 mutex_unlock(&vs->dev.mutex);
1430 return 0;
1431}
1432
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001433static void vhost_scsi_free(struct vhost_scsi *vs)
1434{
1435 if (is_vmalloc_addr(vs))
1436 vfree(vs);
1437 else
1438 kfree(vs);
1439}
1440
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001441static int vhost_scsi_open(struct inode *inode, struct file *f)
1442{
Asias Hec7289312013-05-06 16:38:26 +08001443 struct vhost_scsi *vs;
Asias He3ab2e422013-04-27 11:16:48 +08001444 struct vhost_virtqueue **vqs;
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001445 int r = -ENOMEM, i;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001446
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001447 vs = kzalloc(sizeof(*vs), GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
1448 if (!vs) {
1449 vs = vzalloc(sizeof(*vs));
1450 if (!vs)
1451 goto err_vs;
1452 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001453
Asias He3ab2e422013-04-27 11:16:48 +08001454 vqs = kmalloc(VHOST_SCSI_MAX_VQ * sizeof(*vqs), GFP_KERNEL);
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001455 if (!vqs)
1456 goto err_vqs;
Asias He3ab2e422013-04-27 11:16:48 +08001457
Asias Hec7289312013-05-06 16:38:26 +08001458 vhost_work_init(&vs->vs_completion_work, vhost_scsi_complete_cmd_work);
1459 vhost_work_init(&vs->vs_event_work, tcm_vhost_evt_work);
Asias Hea6c9af82013-04-25 15:35:21 +08001460
Asias Hec7289312013-05-06 16:38:26 +08001461 vs->vs_events_nr = 0;
1462 vs->vs_events_missed = false;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001463
Asias Hec7289312013-05-06 16:38:26 +08001464 vqs[VHOST_SCSI_VQ_CTL] = &vs->vqs[VHOST_SCSI_VQ_CTL].vq;
1465 vqs[VHOST_SCSI_VQ_EVT] = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1466 vs->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick;
1467 vs->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick;
Asias He3ab2e422013-04-27 11:16:48 +08001468 for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) {
Asias Hec7289312013-05-06 16:38:26 +08001469 vqs[i] = &vs->vqs[i].vq;
1470 vs->vqs[i].vq.handle_kick = vhost_scsi_handle_kick;
Asias He3ab2e422013-04-27 11:16:48 +08001471 }
Zhi Yong Wu59566b6e2013-12-07 04:13:03 +08001472 vhost_dev_init(&vs->dev, vqs, VHOST_SCSI_MAX_VQ);
Asias Hef2f0173d2013-04-27 11:16:49 +08001473
Asias Hec7289312013-05-06 16:38:26 +08001474 tcm_vhost_init_inflight(vs, NULL);
Asias Hef2f0173d2013-04-27 11:16:49 +08001475
Asias Hec7289312013-05-06 16:38:26 +08001476 f->private_data = vs;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001477 return 0;
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001478
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001479err_vqs:
1480 vhost_scsi_free(vs);
1481err_vs:
1482 return r;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001483}
1484
1485static int vhost_scsi_release(struct inode *inode, struct file *f)
1486{
Asias Hec7289312013-05-06 16:38:26 +08001487 struct vhost_scsi *vs = f->private_data;
Asias He67e18cf2013-02-05 12:31:57 +08001488 struct vhost_scsi_target t;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001489
Asias Hec7289312013-05-06 16:38:26 +08001490 mutex_lock(&vs->dev.mutex);
1491 memcpy(t.vhost_wwpn, vs->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
1492 mutex_unlock(&vs->dev.mutex);
1493 vhost_scsi_clear_endpoint(vs, &t);
1494 vhost_dev_stop(&vs->dev);
1495 vhost_dev_cleanup(&vs->dev, false);
Asias Hea6c9af82013-04-25 15:35:21 +08001496 /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
Asias Hec7289312013-05-06 16:38:26 +08001497 vhost_scsi_flush(vs);
1498 kfree(vs->dev.vqs);
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001499 vhost_scsi_free(vs);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001500 return 0;
1501}
1502
Asias He683bd962013-05-06 16:38:27 +08001503static long
1504vhost_scsi_ioctl(struct file *f,
1505 unsigned int ioctl,
1506 unsigned long arg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001507{
1508 struct vhost_scsi *vs = f->private_data;
1509 struct vhost_scsi_target backend;
1510 void __user *argp = (void __user *)arg;
1511 u64 __user *featurep = argp;
Asias He11c63412013-04-25 15:35:22 +08001512 u32 __user *eventsp = argp;
1513 u32 events_missed;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001514 u64 features;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001515 int r, abi_version = VHOST_SCSI_ABI_VERSION;
Asias He3ab2e422013-04-27 11:16:48 +08001516 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001517
1518 switch (ioctl) {
1519 case VHOST_SCSI_SET_ENDPOINT:
1520 if (copy_from_user(&backend, argp, sizeof backend))
1521 return -EFAULT;
Michael S. Tsirkin6de71452012-08-18 15:44:09 -07001522 if (backend.reserved != 0)
1523 return -EOPNOTSUPP;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001524
1525 return vhost_scsi_set_endpoint(vs, &backend);
1526 case VHOST_SCSI_CLEAR_ENDPOINT:
1527 if (copy_from_user(&backend, argp, sizeof backend))
1528 return -EFAULT;
Michael S. Tsirkin6de71452012-08-18 15:44:09 -07001529 if (backend.reserved != 0)
1530 return -EOPNOTSUPP;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001531
1532 return vhost_scsi_clear_endpoint(vs, &backend);
1533 case VHOST_SCSI_GET_ABI_VERSION:
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001534 if (copy_to_user(argp, &abi_version, sizeof abi_version))
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001535 return -EFAULT;
1536 return 0;
Asias He11c63412013-04-25 15:35:22 +08001537 case VHOST_SCSI_SET_EVENTS_MISSED:
1538 if (get_user(events_missed, eventsp))
1539 return -EFAULT;
1540 mutex_lock(&vq->mutex);
1541 vs->vs_events_missed = events_missed;
1542 mutex_unlock(&vq->mutex);
1543 return 0;
1544 case VHOST_SCSI_GET_EVENTS_MISSED:
1545 mutex_lock(&vq->mutex);
1546 events_missed = vs->vs_events_missed;
1547 mutex_unlock(&vq->mutex);
1548 if (put_user(events_missed, eventsp))
1549 return -EFAULT;
1550 return 0;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001551 case VHOST_GET_FEATURES:
Nicholas Bellinger5dade712013-03-27 17:23:41 -07001552 features = VHOST_SCSI_FEATURES;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001553 if (copy_to_user(featurep, &features, sizeof features))
1554 return -EFAULT;
1555 return 0;
1556 case VHOST_SET_FEATURES:
1557 if (copy_from_user(&features, featurep, sizeof features))
1558 return -EFAULT;
1559 return vhost_scsi_set_features(vs, features);
1560 default:
1561 mutex_lock(&vs->dev.mutex);
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001562 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
1563 /* TODO: flush backend after dev ioctl. */
1564 if (r == -ENOIOCTLCMD)
1565 r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001566 mutex_unlock(&vs->dev.mutex);
1567 return r;
1568 }
1569}
1570
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001571#ifdef CONFIG_COMPAT
1572static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
1573 unsigned long arg)
1574{
1575 return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1576}
1577#endif
1578
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001579static const struct file_operations vhost_scsi_fops = {
1580 .owner = THIS_MODULE,
1581 .release = vhost_scsi_release,
1582 .unlocked_ioctl = vhost_scsi_ioctl,
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001583#ifdef CONFIG_COMPAT
1584 .compat_ioctl = vhost_scsi_compat_ioctl,
1585#endif
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001586 .open = vhost_scsi_open,
1587 .llseek = noop_llseek,
1588};
1589
1590static struct miscdevice vhost_scsi_misc = {
1591 MISC_DYNAMIC_MINOR,
1592 "vhost-scsi",
1593 &vhost_scsi_fops,
1594};
1595
1596static int __init vhost_scsi_register(void)
1597{
1598 return misc_register(&vhost_scsi_misc);
1599}
1600
1601static int vhost_scsi_deregister(void)
1602{
1603 return misc_deregister(&vhost_scsi_misc);
1604}
1605
1606static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
1607{
1608 switch (tport->tport_proto_id) {
1609 case SCSI_PROTOCOL_SAS:
1610 return "SAS";
1611 case SCSI_PROTOCOL_FCP:
1612 return "FCP";
1613 case SCSI_PROTOCOL_ISCSI:
1614 return "iSCSI";
1615 default:
1616 break;
1617 }
1618
1619 return "Unknown";
1620}
1621
Asias He683bd962013-05-06 16:38:27 +08001622static void
1623tcm_vhost_do_plug(struct tcm_vhost_tpg *tpg,
1624 struct se_lun *lun, bool plug)
Asias Hea6c9af82013-04-25 15:35:21 +08001625{
1626
1627 struct vhost_scsi *vs = tpg->vhost_scsi;
1628 struct vhost_virtqueue *vq;
1629 u32 reason;
1630
1631 if (!vs)
1632 return;
1633
1634 mutex_lock(&vs->dev.mutex);
1635 if (!vhost_has_feature(&vs->dev, VIRTIO_SCSI_F_HOTPLUG)) {
1636 mutex_unlock(&vs->dev.mutex);
1637 return;
1638 }
1639
1640 if (plug)
1641 reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
1642 else
1643 reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
1644
Asias He3ab2e422013-04-27 11:16:48 +08001645 vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Asias Hea6c9af82013-04-25 15:35:21 +08001646 mutex_lock(&vq->mutex);
1647 tcm_vhost_send_evt(vs, tpg, lun,
1648 VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
1649 mutex_unlock(&vq->mutex);
1650 mutex_unlock(&vs->dev.mutex);
1651}
1652
1653static void tcm_vhost_hotplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1654{
1655 tcm_vhost_do_plug(tpg, lun, true);
1656}
1657
1658static void tcm_vhost_hotunplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1659{
1660 tcm_vhost_do_plug(tpg, lun, false);
1661}
1662
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001663static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08001664 struct se_lun *lun)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001665{
Asias He98718312013-05-06 16:38:28 +08001666 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001667 struct tcm_vhost_tpg, se_tpg);
1668
Asias Hea6c9af82013-04-25 15:35:21 +08001669 mutex_lock(&tcm_vhost_mutex);
1670
Asias He98718312013-05-06 16:38:28 +08001671 mutex_lock(&tpg->tv_tpg_mutex);
1672 tpg->tv_tpg_port_count++;
1673 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001674
Asias He98718312013-05-06 16:38:28 +08001675 tcm_vhost_hotplug(tpg, lun);
Asias Hea6c9af82013-04-25 15:35:21 +08001676
1677 mutex_unlock(&tcm_vhost_mutex);
1678
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001679 return 0;
1680}
1681
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001682static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08001683 struct se_lun *lun)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001684{
Asias He98718312013-05-06 16:38:28 +08001685 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001686 struct tcm_vhost_tpg, se_tpg);
1687
Asias Hea6c9af82013-04-25 15:35:21 +08001688 mutex_lock(&tcm_vhost_mutex);
1689
Asias He98718312013-05-06 16:38:28 +08001690 mutex_lock(&tpg->tv_tpg_mutex);
1691 tpg->tv_tpg_port_count--;
1692 mutex_unlock(&tpg->tv_tpg_mutex);
Asias Hea6c9af82013-04-25 15:35:21 +08001693
Asias He98718312013-05-06 16:38:28 +08001694 tcm_vhost_hotunplug(tpg, lun);
Asias Hea6c9af82013-04-25 15:35:21 +08001695
1696 mutex_unlock(&tcm_vhost_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001697}
1698
Asias He683bd962013-05-06 16:38:27 +08001699static struct se_node_acl *
1700tcm_vhost_make_nodeacl(struct se_portal_group *se_tpg,
1701 struct config_group *group,
1702 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001703{
1704 struct se_node_acl *se_nacl, *se_nacl_new;
1705 struct tcm_vhost_nacl *nacl;
1706 u64 wwpn = 0;
1707 u32 nexus_depth;
1708
1709 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1710 return ERR_PTR(-EINVAL); */
1711 se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
1712 if (!se_nacl_new)
1713 return ERR_PTR(-ENOMEM);
1714
1715 nexus_depth = 1;
1716 /*
1717 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1718 * when converting a NodeACL from demo mode -> explict
1719 */
1720 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1721 name, nexus_depth);
1722 if (IS_ERR(se_nacl)) {
1723 tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
1724 return se_nacl;
1725 }
1726 /*
1727 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1728 */
1729 nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
1730 nacl->iport_wwpn = wwpn;
1731
1732 return se_nacl;
1733}
1734
1735static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
1736{
1737 struct tcm_vhost_nacl *nacl = container_of(se_acl,
1738 struct tcm_vhost_nacl, se_node_acl);
1739 core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1740 kfree(nacl);
1741}
1742
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001743static void tcm_vhost_free_cmd_map_res(struct tcm_vhost_nexus *nexus,
1744 struct se_session *se_sess)
1745{
1746 struct tcm_vhost_cmd *tv_cmd;
1747 unsigned int i;
1748
1749 if (!se_sess->sess_cmd_map)
1750 return;
1751
1752 for (i = 0; i < TCM_VHOST_DEFAULT_TAGS; i++) {
1753 tv_cmd = &((struct tcm_vhost_cmd *)se_sess->sess_cmd_map)[i];
1754
1755 kfree(tv_cmd->tvc_sgl);
Nicholas Bellingerb1935f62014-02-22 18:08:24 -08001756 kfree(tv_cmd->tvc_prot_sgl);
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001757 kfree(tv_cmd->tvc_upages);
1758 }
1759}
1760
Asias He98718312013-05-06 16:38:28 +08001761static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tpg,
Asias He683bd962013-05-06 16:38:27 +08001762 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001763{
1764 struct se_portal_group *se_tpg;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001765 struct se_session *se_sess;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001766 struct tcm_vhost_nexus *tv_nexus;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001767 struct tcm_vhost_cmd *tv_cmd;
1768 unsigned int i;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001769
Asias He98718312013-05-06 16:38:28 +08001770 mutex_lock(&tpg->tv_tpg_mutex);
1771 if (tpg->tpg_nexus) {
1772 mutex_unlock(&tpg->tv_tpg_mutex);
1773 pr_debug("tpg->tpg_nexus already exists\n");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001774 return -EEXIST;
1775 }
Asias He98718312013-05-06 16:38:28 +08001776 se_tpg = &tpg->se_tpg;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001777
1778 tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
1779 if (!tv_nexus) {
Asias He98718312013-05-06 16:38:28 +08001780 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001781 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1782 return -ENOMEM;
1783 }
1784 /*
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -07001785 * Initialize the struct se_session pointer and setup tagpool
1786 * for struct tcm_vhost_cmd descriptors
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001787 */
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -07001788 tv_nexus->tvn_se_sess = transport_init_session_tags(
1789 TCM_VHOST_DEFAULT_TAGS,
Nicholas Bellingere70beee2014-04-02 12:52:38 -07001790 sizeof(struct tcm_vhost_cmd),
1791 TARGET_PROT_NORMAL);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001792 if (IS_ERR(tv_nexus->tvn_se_sess)) {
Asias He98718312013-05-06 16:38:28 +08001793 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001794 kfree(tv_nexus);
1795 return -ENOMEM;
1796 }
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001797 se_sess = tv_nexus->tvn_se_sess;
1798 for (i = 0; i < TCM_VHOST_DEFAULT_TAGS; i++) {
1799 tv_cmd = &((struct tcm_vhost_cmd *)se_sess->sess_cmd_map)[i];
1800
1801 tv_cmd->tvc_sgl = kzalloc(sizeof(struct scatterlist) *
1802 TCM_VHOST_PREALLOC_SGLS, GFP_KERNEL);
1803 if (!tv_cmd->tvc_sgl) {
1804 mutex_unlock(&tpg->tv_tpg_mutex);
1805 pr_err("Unable to allocate tv_cmd->tvc_sgl\n");
1806 goto out;
1807 }
1808
1809 tv_cmd->tvc_upages = kzalloc(sizeof(struct page *) *
Nicholas Bellinger5a01d082014-02-22 18:34:08 -08001810 TCM_VHOST_PREALLOC_UPAGES, GFP_KERNEL);
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001811 if (!tv_cmd->tvc_upages) {
1812 mutex_unlock(&tpg->tv_tpg_mutex);
1813 pr_err("Unable to allocate tv_cmd->tvc_upages\n");
1814 goto out;
1815 }
Nicholas Bellingerb1935f62014-02-22 18:08:24 -08001816
1817 tv_cmd->tvc_prot_sgl = kzalloc(sizeof(struct scatterlist) *
1818 TCM_VHOST_PREALLOC_PROT_SGLS, GFP_KERNEL);
1819 if (!tv_cmd->tvc_prot_sgl) {
1820 mutex_unlock(&tpg->tv_tpg_mutex);
1821 pr_err("Unable to allocate tv_cmd->tvc_prot_sgl\n");
1822 goto out;
1823 }
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001824 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001825 /*
1826 * Since we are running in 'demo mode' this call with generate a
1827 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1828 * the SCSI Initiator port name of the passed configfs group 'name'.
1829 */
1830 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1831 se_tpg, (unsigned char *)name);
1832 if (!tv_nexus->tvn_se_sess->se_node_acl) {
Asias He98718312013-05-06 16:38:28 +08001833 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001834 pr_debug("core_tpg_check_initiator_node_acl() failed"
1835 " for %s\n", name);
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001836 goto out;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001837 }
1838 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001839 * Now register the TCM vhost virtual I_T Nexus as active with the
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001840 * call to __transport_register_session()
1841 */
1842 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1843 tv_nexus->tvn_se_sess, tv_nexus);
Asias He98718312013-05-06 16:38:28 +08001844 tpg->tpg_nexus = tv_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001845
Asias He98718312013-05-06 16:38:28 +08001846 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001847 return 0;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001848
1849out:
1850 tcm_vhost_free_cmd_map_res(tv_nexus, se_sess);
1851 transport_free_session(se_sess);
1852 kfree(tv_nexus);
1853 return -ENOMEM;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001854}
1855
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001856static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001857{
1858 struct se_session *se_sess;
1859 struct tcm_vhost_nexus *tv_nexus;
1860
1861 mutex_lock(&tpg->tv_tpg_mutex);
1862 tv_nexus = tpg->tpg_nexus;
1863 if (!tv_nexus) {
1864 mutex_unlock(&tpg->tv_tpg_mutex);
1865 return -ENODEV;
1866 }
1867
1868 se_sess = tv_nexus->tvn_se_sess;
1869 if (!se_sess) {
1870 mutex_unlock(&tpg->tv_tpg_mutex);
1871 return -ENODEV;
1872 }
1873
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001874 if (tpg->tv_tpg_port_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001875 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001876 pr_err("Unable to remove TCM_vhost I_T Nexus with"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001877 " active TPG port count: %d\n",
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001878 tpg->tv_tpg_port_count);
1879 return -EBUSY;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001880 }
1881
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001882 if (tpg->tv_tpg_vhost_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001883 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001884 pr_err("Unable to remove TCM_vhost I_T Nexus with"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001885 " active TPG vhost count: %d\n",
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001886 tpg->tv_tpg_vhost_count);
1887 return -EBUSY;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001888 }
1889
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001890 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001891 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
1892 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001893
1894 tcm_vhost_free_cmd_map_res(tv_nexus, se_sess);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001895 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001896 * Release the SCSI I_T Nexus to the emulated vhost Target Port
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001897 */
1898 transport_deregister_session(tv_nexus->tvn_se_sess);
1899 tpg->tpg_nexus = NULL;
1900 mutex_unlock(&tpg->tv_tpg_mutex);
1901
1902 kfree(tv_nexus);
1903 return 0;
1904}
1905
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001906static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08001907 char *page)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001908{
Asias He98718312013-05-06 16:38:28 +08001909 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001910 struct tcm_vhost_tpg, se_tpg);
1911 struct tcm_vhost_nexus *tv_nexus;
1912 ssize_t ret;
1913
Asias He98718312013-05-06 16:38:28 +08001914 mutex_lock(&tpg->tv_tpg_mutex);
1915 tv_nexus = tpg->tpg_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001916 if (!tv_nexus) {
Asias He98718312013-05-06 16:38:28 +08001917 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001918 return -ENODEV;
1919 }
1920 ret = snprintf(page, PAGE_SIZE, "%s\n",
1921 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
Asias He98718312013-05-06 16:38:28 +08001922 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001923
1924 return ret;
1925}
1926
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001927static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08001928 const char *page,
1929 size_t count)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001930{
Asias He98718312013-05-06 16:38:28 +08001931 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001932 struct tcm_vhost_tpg, se_tpg);
Asias He98718312013-05-06 16:38:28 +08001933 struct tcm_vhost_tport *tport_wwn = tpg->tport;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001934 unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
1935 int ret;
1936 /*
1937 * Shutdown the active I_T nexus if 'NULL' is passed..
1938 */
1939 if (!strncmp(page, "NULL", 4)) {
Asias He98718312013-05-06 16:38:28 +08001940 ret = tcm_vhost_drop_nexus(tpg);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001941 return (!ret) ? count : ret;
1942 }
1943 /*
1944 * Otherwise make sure the passed virtual Initiator port WWN matches
1945 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
1946 * tcm_vhost_make_nexus().
1947 */
1948 if (strlen(page) >= TCM_VHOST_NAMELEN) {
1949 pr_err("Emulated NAA Sas Address: %s, exceeds"
1950 " max: %d\n", page, TCM_VHOST_NAMELEN);
1951 return -EINVAL;
1952 }
1953 snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
1954
1955 ptr = strstr(i_port, "naa.");
1956 if (ptr) {
1957 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1958 pr_err("Passed SAS Initiator Port %s does not"
1959 " match target port protoid: %s\n", i_port,
1960 tcm_vhost_dump_proto_id(tport_wwn));
1961 return -EINVAL;
1962 }
1963 port_ptr = &i_port[0];
1964 goto check_newline;
1965 }
1966 ptr = strstr(i_port, "fc.");
1967 if (ptr) {
1968 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1969 pr_err("Passed FCP Initiator Port %s does not"
1970 " match target port protoid: %s\n", i_port,
1971 tcm_vhost_dump_proto_id(tport_wwn));
1972 return -EINVAL;
1973 }
1974 port_ptr = &i_port[3]; /* Skip over "fc." */
1975 goto check_newline;
1976 }
1977 ptr = strstr(i_port, "iqn.");
1978 if (ptr) {
1979 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1980 pr_err("Passed iSCSI Initiator Port %s does not"
1981 " match target port protoid: %s\n", i_port,
1982 tcm_vhost_dump_proto_id(tport_wwn));
1983 return -EINVAL;
1984 }
1985 port_ptr = &i_port[0];
1986 goto check_newline;
1987 }
1988 pr_err("Unable to locate prefix for emulated Initiator Port:"
1989 " %s\n", i_port);
1990 return -EINVAL;
1991 /*
1992 * Clear any trailing newline for the NAA WWN
1993 */
1994check_newline:
1995 if (i_port[strlen(i_port)-1] == '\n')
1996 i_port[strlen(i_port)-1] = '\0';
1997
Asias He98718312013-05-06 16:38:28 +08001998 ret = tcm_vhost_make_nexus(tpg, port_ptr);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001999 if (ret < 0)
2000 return ret;
2001
2002 return count;
2003}
2004
2005TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
2006
2007static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
2008 &tcm_vhost_tpg_nexus.attr,
2009 NULL,
2010};
2011
Asias He683bd962013-05-06 16:38:27 +08002012static struct se_portal_group *
2013tcm_vhost_make_tpg(struct se_wwn *wwn,
2014 struct config_group *group,
2015 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002016{
2017 struct tcm_vhost_tport *tport = container_of(wwn,
2018 struct tcm_vhost_tport, tport_wwn);
2019
2020 struct tcm_vhost_tpg *tpg;
2021 unsigned long tpgt;
2022 int ret;
2023
2024 if (strstr(name, "tpgt_") != name)
2025 return ERR_PTR(-EINVAL);
2026 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
2027 return ERR_PTR(-EINVAL);
2028
2029 tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
2030 if (!tpg) {
2031 pr_err("Unable to allocate struct tcm_vhost_tpg");
2032 return ERR_PTR(-ENOMEM);
2033 }
2034 mutex_init(&tpg->tv_tpg_mutex);
2035 INIT_LIST_HEAD(&tpg->tv_tpg_list);
2036 tpg->tport = tport;
2037 tpg->tport_tpgt = tpgt;
2038
2039 ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
2040 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
2041 if (ret < 0) {
2042 kfree(tpg);
2043 return NULL;
2044 }
2045 mutex_lock(&tcm_vhost_mutex);
2046 list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
2047 mutex_unlock(&tcm_vhost_mutex);
2048
2049 return &tpg->se_tpg;
2050}
2051
2052static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
2053{
2054 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
2055 struct tcm_vhost_tpg, se_tpg);
2056
2057 mutex_lock(&tcm_vhost_mutex);
2058 list_del(&tpg->tv_tpg_list);
2059 mutex_unlock(&tcm_vhost_mutex);
2060 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002061 * Release the virtual I_T Nexus for this vhost TPG
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002062 */
2063 tcm_vhost_drop_nexus(tpg);
2064 /*
2065 * Deregister the se_tpg from TCM..
2066 */
2067 core_tpg_deregister(se_tpg);
2068 kfree(tpg);
2069}
2070
Asias He683bd962013-05-06 16:38:27 +08002071static struct se_wwn *
2072tcm_vhost_make_tport(struct target_fabric_configfs *tf,
2073 struct config_group *group,
2074 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002075{
2076 struct tcm_vhost_tport *tport;
2077 char *ptr;
2078 u64 wwpn = 0;
2079 int off = 0;
2080
2081 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
2082 return ERR_PTR(-EINVAL); */
2083
2084 tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
2085 if (!tport) {
2086 pr_err("Unable to allocate struct tcm_vhost_tport");
2087 return ERR_PTR(-ENOMEM);
2088 }
2089 tport->tport_wwpn = wwpn;
2090 /*
2091 * Determine the emulated Protocol Identifier and Target Port Name
2092 * based on the incoming configfs directory name.
2093 */
2094 ptr = strstr(name, "naa.");
2095 if (ptr) {
2096 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
2097 goto check_len;
2098 }
2099 ptr = strstr(name, "fc.");
2100 if (ptr) {
2101 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
2102 off = 3; /* Skip over "fc." */
2103 goto check_len;
2104 }
2105 ptr = strstr(name, "iqn.");
2106 if (ptr) {
2107 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
2108 goto check_len;
2109 }
2110
2111 pr_err("Unable to locate prefix for emulated Target Port:"
2112 " %s\n", name);
2113 kfree(tport);
2114 return ERR_PTR(-EINVAL);
2115
2116check_len:
2117 if (strlen(name) >= TCM_VHOST_NAMELEN) {
2118 pr_err("Emulated %s Address: %s, exceeds"
2119 " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
2120 TCM_VHOST_NAMELEN);
2121 kfree(tport);
2122 return ERR_PTR(-EINVAL);
2123 }
2124 snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
2125
2126 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
2127 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
2128
2129 return &tport->tport_wwn;
2130}
2131
2132static void tcm_vhost_drop_tport(struct se_wwn *wwn)
2133{
2134 struct tcm_vhost_tport *tport = container_of(wwn,
2135 struct tcm_vhost_tport, tport_wwn);
2136
2137 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
2138 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
2139 tport->tport_name);
2140
2141 kfree(tport);
2142}
2143
Asias He683bd962013-05-06 16:38:27 +08002144static ssize_t
2145tcm_vhost_wwn_show_attr_version(struct target_fabric_configfs *tf,
2146 char *page)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002147{
2148 return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
2149 "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
2150 utsname()->machine);
2151}
2152
2153TF_WWN_ATTR_RO(tcm_vhost, version);
2154
2155static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
2156 &tcm_vhost_wwn_version.attr,
2157 NULL,
2158};
2159
2160static struct target_core_fabric_ops tcm_vhost_ops = {
2161 .get_fabric_name = tcm_vhost_get_fabric_name,
2162 .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
2163 .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
2164 .tpg_get_tag = tcm_vhost_get_tag,
2165 .tpg_get_default_depth = tcm_vhost_get_default_depth,
2166 .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
2167 .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
2168 .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
2169 .tpg_check_demo_mode = tcm_vhost_check_true,
2170 .tpg_check_demo_mode_cache = tcm_vhost_check_true,
2171 .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
2172 .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
2173 .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
2174 .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
2175 .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
2176 .release_cmd = tcm_vhost_release_cmd,
Nicholas Bellinger084ed452013-06-06 02:20:41 -07002177 .check_stop_free = vhost_scsi_check_stop_free,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002178 .shutdown_session = tcm_vhost_shutdown_session,
2179 .close_session = tcm_vhost_close_session,
2180 .sess_get_index = tcm_vhost_sess_get_index,
2181 .sess_get_initiator_sid = NULL,
2182 .write_pending = tcm_vhost_write_pending,
2183 .write_pending_status = tcm_vhost_write_pending_status,
2184 .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
2185 .get_task_tag = tcm_vhost_get_task_tag,
2186 .get_cmd_state = tcm_vhost_get_cmd_state,
2187 .queue_data_in = tcm_vhost_queue_data_in,
2188 .queue_status = tcm_vhost_queue_status,
2189 .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002190 .aborted_task = tcm_vhost_aborted_task,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002191 /*
2192 * Setup callers for generic logic in target_core_fabric_configfs.c
2193 */
2194 .fabric_make_wwn = tcm_vhost_make_tport,
2195 .fabric_drop_wwn = tcm_vhost_drop_tport,
2196 .fabric_make_tpg = tcm_vhost_make_tpg,
2197 .fabric_drop_tpg = tcm_vhost_drop_tpg,
2198 .fabric_post_link = tcm_vhost_port_link,
2199 .fabric_pre_unlink = tcm_vhost_port_unlink,
2200 .fabric_make_np = NULL,
2201 .fabric_drop_np = NULL,
2202 .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
2203 .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
2204};
2205
2206static int tcm_vhost_register_configfs(void)
2207{
2208 struct target_fabric_configfs *fabric;
2209 int ret;
2210
2211 pr_debug("TCM_VHOST fabric module %s on %s/%s"
2212 " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
2213 utsname()->machine);
2214 /*
2215 * Register the top level struct config_item_type with TCM core
2216 */
2217 fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
2218 if (IS_ERR(fabric)) {
2219 pr_err("target_fabric_configfs_init() failed\n");
2220 return PTR_ERR(fabric);
2221 }
2222 /*
2223 * Setup fabric->tf_ops from our local tcm_vhost_ops
2224 */
2225 fabric->tf_ops = tcm_vhost_ops;
2226 /*
2227 * Setup default attribute lists for various fabric->tf_cit_tmpl
2228 */
Andy Groverd80e224d2013-10-09 11:05:56 -07002229 fabric->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
2230 fabric->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
2231 fabric->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = NULL;
2232 fabric->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL;
2233 fabric->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
2234 fabric->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL;
2235 fabric->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
2236 fabric->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
2237 fabric->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002238 /*
2239 * Register the fabric for use within TCM
2240 */
2241 ret = target_fabric_configfs_register(fabric);
2242 if (ret < 0) {
2243 pr_err("target_fabric_configfs_register() failed"
2244 " for TCM_VHOST\n");
2245 return ret;
2246 }
2247 /*
2248 * Setup our local pointer to *fabric
2249 */
2250 tcm_vhost_fabric_configfs = fabric;
2251 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
2252 return 0;
2253};
2254
2255static void tcm_vhost_deregister_configfs(void)
2256{
2257 if (!tcm_vhost_fabric_configfs)
2258 return;
2259
2260 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
2261 tcm_vhost_fabric_configfs = NULL;
2262 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
2263};
2264
2265static int __init tcm_vhost_init(void)
2266{
2267 int ret = -ENOMEM;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002268 /*
2269 * Use our own dedicated workqueue for submitting I/O into
2270 * target core to avoid contention within system_wq.
2271 */
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002272 tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
2273 if (!tcm_vhost_workqueue)
2274 goto out;
2275
2276 ret = vhost_scsi_register();
2277 if (ret < 0)
2278 goto out_destroy_workqueue;
2279
2280 ret = tcm_vhost_register_configfs();
2281 if (ret < 0)
2282 goto out_vhost_scsi_deregister;
2283
2284 return 0;
2285
2286out_vhost_scsi_deregister:
2287 vhost_scsi_deregister();
2288out_destroy_workqueue:
2289 destroy_workqueue(tcm_vhost_workqueue);
2290out:
2291 return ret;
2292};
2293
2294static void tcm_vhost_exit(void)
2295{
2296 tcm_vhost_deregister_configfs();
2297 vhost_scsi_deregister();
2298 destroy_workqueue(tcm_vhost_workqueue);
2299};
2300
Michael S. Tsirkin181c04a2013-05-02 03:52:59 +03002301MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
2302MODULE_ALIAS("tcm_vhost");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002303MODULE_LICENSE("GPL");
2304module_init(tcm_vhost_init);
2305module_exit(tcm_vhost_exit);