blob: 8cd545ad3f51a417c4500f5f489beac1d7791c6c [file] [log] [blame]
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001/*******************************************************************************
2 * Vhost kernel TCM fabric driver for virtio SCSI initiators
3 *
4 * (C) Copyright 2010-2012 RisingTide Systems LLC.
5 * (C) Copyright 2010-2012 IBM Corp.
6 *
7 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
8 *
9 * Authors: Nicholas A. Bellinger <nab@risingtidesystems.com>
10 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 ****************************************************************************/
23
24#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <generated/utsrelease.h>
27#include <linux/utsname.h>
28#include <linux/init.h>
29#include <linux/slab.h>
30#include <linux/kthread.h>
31#include <linux/types.h>
32#include <linux/string.h>
33#include <linux/configfs.h>
34#include <linux/ctype.h>
35#include <linux/compat.h>
36#include <linux/eventfd.h>
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070037#include <linux/fs.h>
38#include <linux/miscdevice.h>
39#include <asm/unaligned.h>
40#include <scsi/scsi.h>
41#include <scsi/scsi_tcq.h>
42#include <target/target_core_base.h>
43#include <target/target_core_fabric.h>
44#include <target/target_core_fabric_configfs.h>
45#include <target/target_core_configfs.h>
46#include <target/configfs_macros.h>
47#include <linux/vhost.h>
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
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +030059
60struct vhost_scsi_inflight {
61 /* Wait for the flush operation to finish */
62 struct completion comp;
63 /* Refcount for the inflight reqs */
64 struct kref kref;
65};
66
67struct tcm_vhost_cmd {
68 /* Descriptor from vhost_get_vq_desc() for virt_queue segment */
69 int tvc_vq_desc;
70 /* virtio-scsi initiator task attribute */
71 int tvc_task_attr;
72 /* virtio-scsi initiator data direction */
73 enum dma_data_direction tvc_data_direction;
74 /* Expected data transfer length from virtio-scsi header */
75 u32 tvc_exp_data_len;
76 /* The Tag from include/linux/virtio_scsi.h:struct virtio_scsi_cmd_req */
77 u64 tvc_tag;
78 /* The number of scatterlists associated with this cmd */
79 u32 tvc_sgl_count;
80 /* Saved unpacked SCSI LUN for tcm_vhost_submission_work() */
81 u32 tvc_lun;
82 /* Pointer to the SGL formatted memory from virtio-scsi */
83 struct scatterlist *tvc_sgl;
84 /* Pointer to response */
85 struct virtio_scsi_cmd_resp __user *tvc_resp;
86 /* Pointer to vhost_scsi for our device */
87 struct vhost_scsi *tvc_vhost;
88 /* Pointer to vhost_virtqueue for the cmd */
89 struct vhost_virtqueue *tvc_vq;
90 /* Pointer to vhost nexus memory */
91 struct tcm_vhost_nexus *tvc_nexus;
92 /* The TCM I/O descriptor that is accessed via container_of() */
93 struct se_cmd tvc_se_cmd;
94 /* work item used for cmwq dispatch to tcm_vhost_submission_work() */
95 struct work_struct work;
96 /* Copy of the incoming SCSI command descriptor block (CDB) */
97 unsigned char tvc_cdb[TCM_VHOST_MAX_CDB_SIZE];
98 /* Sense buffer that will be mapped into outgoing status */
99 unsigned char tvc_sense_buf[TRANSPORT_SENSE_BUFFER];
100 /* Completed commands list, serviced from vhost worker thread */
101 struct llist_node tvc_completion_list;
102 /* Used to track inflight cmd */
103 struct vhost_scsi_inflight *inflight;
104};
105
106struct tcm_vhost_nexus {
107 /* Pointer to TCM session for I_T Nexus */
108 struct se_session *tvn_se_sess;
109};
110
111struct tcm_vhost_nacl {
112 /* Binary World Wide unique Port Name for Vhost Initiator port */
113 u64 iport_wwpn;
114 /* ASCII formatted WWPN for Sas Initiator port */
115 char iport_name[TCM_VHOST_NAMELEN];
116 /* Returned by tcm_vhost_make_nodeacl() */
117 struct se_node_acl se_node_acl;
118};
119
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300120struct tcm_vhost_tpg {
121 /* Vhost port target portal group tag for TCM */
122 u16 tport_tpgt;
123 /* Used to track number of TPG Port/Lun Links wrt to explict I_T Nexus shutdown */
124 int tv_tpg_port_count;
125 /* Used for vhost_scsi device reference to tpg_nexus, protected by tv_tpg_mutex */
126 int tv_tpg_vhost_count;
127 /* list for tcm_vhost_list */
128 struct list_head tv_tpg_list;
129 /* Used to protect access for tpg_nexus */
130 struct mutex tv_tpg_mutex;
131 /* Pointer to the TCM VHost I_T Nexus for this TPG endpoint */
132 struct tcm_vhost_nexus *tpg_nexus;
133 /* Pointer back to tcm_vhost_tport */
134 struct tcm_vhost_tport *tport;
135 /* Returned by tcm_vhost_make_tpg() */
136 struct se_portal_group se_tpg;
137 /* Pointer back to vhost_scsi, protected by tv_tpg_mutex */
138 struct vhost_scsi *vhost_scsi;
139};
140
141struct tcm_vhost_tport {
142 /* SCSI protocol the tport is providing */
143 u8 tport_proto_id;
144 /* Binary World Wide unique Port Name for Vhost Target port */
145 u64 tport_wwpn;
146 /* ASCII formatted WWPN for Vhost Target port */
147 char tport_name[TCM_VHOST_NAMELEN];
148 /* Returned by tcm_vhost_make_tport() */
149 struct se_wwn tport_wwn;
150};
151
152struct tcm_vhost_evt {
153 /* event to be sent to guest */
154 struct virtio_scsi_event event;
155 /* event list, serviced from vhost worker thread */
156 struct llist_node list;
157};
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700158
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700159enum {
160 VHOST_SCSI_VQ_CTL = 0,
161 VHOST_SCSI_VQ_EVT = 1,
162 VHOST_SCSI_VQ_IO = 2,
163};
164
Nicholas Bellinger5dade712013-03-27 17:23:41 -0700165enum {
Asias Hea18cc422013-05-07 14:51:49 +0800166 VHOST_SCSI_FEATURES = VHOST_FEATURES | (1ULL << VIRTIO_SCSI_F_HOTPLUG)
Nicholas Bellinger5dade712013-03-27 17:23:41 -0700167};
168
Asias He1b7f3902013-02-06 13:20:59 +0800169#define VHOST_SCSI_MAX_TARGET 256
170#define VHOST_SCSI_MAX_VQ 128
Asias Hea6c9af82013-04-25 15:35:21 +0800171#define VHOST_SCSI_MAX_EVENT 128
Asias He67e18cf2013-02-05 12:31:57 +0800172
Asias He3ab2e422013-04-27 11:16:48 +0800173struct vhost_scsi_virtqueue {
174 struct vhost_virtqueue vq;
Michael S. Tsirkin3dfbff32013-04-28 15:38:52 +0300175 /*
176 * Reference counting for inflight reqs, used for flush operation. At
177 * each time, one reference tracks new commands submitted, while we
178 * wait for another one to reach 0.
179 */
Asias Hef2f01732013-04-27 11:16:49 +0800180 struct vhost_scsi_inflight inflights[2];
Michael S. Tsirkin3dfbff32013-04-28 15:38:52 +0300181 /*
182 * Indicate current inflight in use, protected by vq->mutex.
183 * Writers must also take dev mutex and flush under it.
184 */
Asias Hef2f01732013-04-27 11:16:49 +0800185 int inflight_idx;
Asias He3ab2e422013-04-27 11:16:48 +0800186};
187
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700188struct vhost_scsi {
Asias He67e18cf2013-02-05 12:31:57 +0800189 /* Protected by vhost_scsi->dev.mutex */
Asias He4f7f46d2013-04-03 14:17:37 +0800190 struct tcm_vhost_tpg **vs_tpg;
Asias He67e18cf2013-02-05 12:31:57 +0800191 char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
Asias He67e18cf2013-02-05 12:31:57 +0800192
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700193 struct vhost_dev dev;
Asias He3ab2e422013-04-27 11:16:48 +0800194 struct vhost_scsi_virtqueue vqs[VHOST_SCSI_MAX_VQ];
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700195
196 struct vhost_work vs_completion_work; /* cmd completion work item */
Asias He9d6064a2013-01-06 14:36:13 +0800197 struct llist_head vs_completion_list; /* cmd completion queue */
Asias Hea6c9af82013-04-25 15:35:21 +0800198
199 struct vhost_work vs_event_work; /* evt injection work item */
200 struct llist_head vs_event_list; /* evt injection queue */
201
202 bool vs_events_missed; /* any missed events, protected by vq->mutex */
203 int vs_events_nr; /* num of pending events, protected by vq->mutex */
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700204};
205
206/* Local pointer to allocated TCM configfs fabric module */
207static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
208
209static struct workqueue_struct *tcm_vhost_workqueue;
210
211/* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
212static DEFINE_MUTEX(tcm_vhost_mutex);
213static LIST_HEAD(tcm_vhost_list);
214
Asias He765b34f2013-01-22 11:20:25 +0800215static int iov_num_pages(struct iovec *iov)
216{
217 return (PAGE_ALIGN((unsigned long)iov->iov_base + iov->iov_len) -
218 ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
219}
220
Asias He0a1febf2013-06-05 21:17:38 +0800221static void tcm_vhost_done_inflight(struct kref *kref)
Asias Hef2f01732013-04-27 11:16:49 +0800222{
223 struct vhost_scsi_inflight *inflight;
224
225 inflight = container_of(kref, struct vhost_scsi_inflight, kref);
226 complete(&inflight->comp);
227}
228
229static void tcm_vhost_init_inflight(struct vhost_scsi *vs,
230 struct vhost_scsi_inflight *old_inflight[])
231{
232 struct vhost_scsi_inflight *new_inflight;
233 struct vhost_virtqueue *vq;
234 int idx, i;
235
236 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
237 vq = &vs->vqs[i].vq;
238
239 mutex_lock(&vq->mutex);
240
241 /* store old infight */
242 idx = vs->vqs[i].inflight_idx;
243 if (old_inflight)
244 old_inflight[i] = &vs->vqs[i].inflights[idx];
245
246 /* setup new infight */
247 vs->vqs[i].inflight_idx = idx ^ 1;
248 new_inflight = &vs->vqs[i].inflights[idx ^ 1];
249 kref_init(&new_inflight->kref);
250 init_completion(&new_inflight->comp);
251
252 mutex_unlock(&vq->mutex);
253 }
254}
255
256static struct vhost_scsi_inflight *
257tcm_vhost_get_inflight(struct vhost_virtqueue *vq)
258{
259 struct vhost_scsi_inflight *inflight;
260 struct vhost_scsi_virtqueue *svq;
261
262 svq = container_of(vq, struct vhost_scsi_virtqueue, vq);
263 inflight = &svq->inflights[svq->inflight_idx];
264 kref_get(&inflight->kref);
265
266 return inflight;
267}
268
269static void tcm_vhost_put_inflight(struct vhost_scsi_inflight *inflight)
270{
271 kref_put(&inflight->kref, tcm_vhost_done_inflight);
272}
273
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700274static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
275{
276 return 1;
277}
278
279static int tcm_vhost_check_false(struct se_portal_group *se_tpg)
280{
281 return 0;
282}
283
284static char *tcm_vhost_get_fabric_name(void)
285{
286 return "vhost";
287}
288
289static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg)
290{
291 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
292 struct tcm_vhost_tpg, se_tpg);
293 struct tcm_vhost_tport *tport = tpg->tport;
294
295 switch (tport->tport_proto_id) {
296 case SCSI_PROTOCOL_SAS:
297 return sas_get_fabric_proto_ident(se_tpg);
298 case SCSI_PROTOCOL_FCP:
299 return fc_get_fabric_proto_ident(se_tpg);
300 case SCSI_PROTOCOL_ISCSI:
301 return iscsi_get_fabric_proto_ident(se_tpg);
302 default:
303 pr_err("Unknown tport_proto_id: 0x%02x, using"
304 " SAS emulation\n", tport->tport_proto_id);
305 break;
306 }
307
308 return sas_get_fabric_proto_ident(se_tpg);
309}
310
311static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg)
312{
313 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
314 struct tcm_vhost_tpg, se_tpg);
315 struct tcm_vhost_tport *tport = tpg->tport;
316
317 return &tport->tport_name[0];
318}
319
320static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg)
321{
322 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
323 struct tcm_vhost_tpg, se_tpg);
324 return tpg->tport_tpgt;
325}
326
327static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
328{
329 return 1;
330}
331
Asias He683bd962013-05-06 16:38:27 +0800332static u32
333tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
334 struct se_node_acl *se_nacl,
335 struct t10_pr_registration *pr_reg,
336 int *format_code,
337 unsigned char *buf)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700338{
339 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
340 struct tcm_vhost_tpg, se_tpg);
341 struct tcm_vhost_tport *tport = tpg->tport;
342
343 switch (tport->tport_proto_id) {
344 case SCSI_PROTOCOL_SAS:
345 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
346 format_code, buf);
347 case SCSI_PROTOCOL_FCP:
348 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
349 format_code, buf);
350 case SCSI_PROTOCOL_ISCSI:
351 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
352 format_code, buf);
353 default:
354 pr_err("Unknown tport_proto_id: 0x%02x, using"
355 " SAS emulation\n", tport->tport_proto_id);
356 break;
357 }
358
359 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
360 format_code, buf);
361}
362
Asias He683bd962013-05-06 16:38:27 +0800363static u32
364tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
365 struct se_node_acl *se_nacl,
366 struct t10_pr_registration *pr_reg,
367 int *format_code)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700368{
369 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
370 struct tcm_vhost_tpg, se_tpg);
371 struct tcm_vhost_tport *tport = tpg->tport;
372
373 switch (tport->tport_proto_id) {
374 case SCSI_PROTOCOL_SAS:
375 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
376 format_code);
377 case SCSI_PROTOCOL_FCP:
378 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
379 format_code);
380 case SCSI_PROTOCOL_ISCSI:
381 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
382 format_code);
383 default:
384 pr_err("Unknown tport_proto_id: 0x%02x, using"
385 " SAS emulation\n", tport->tport_proto_id);
386 break;
387 }
388
389 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
390 format_code);
391}
392
Asias He683bd962013-05-06 16:38:27 +0800393static char *
394tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
395 const char *buf,
396 u32 *out_tid_len,
397 char **port_nexus_ptr)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700398{
399 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
400 struct tcm_vhost_tpg, se_tpg);
401 struct tcm_vhost_tport *tport = tpg->tport;
402
403 switch (tport->tport_proto_id) {
404 case SCSI_PROTOCOL_SAS:
405 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
406 port_nexus_ptr);
407 case SCSI_PROTOCOL_FCP:
408 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
409 port_nexus_ptr);
410 case SCSI_PROTOCOL_ISCSI:
411 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
412 port_nexus_ptr);
413 default:
414 pr_err("Unknown tport_proto_id: 0x%02x, using"
415 " SAS emulation\n", tport->tport_proto_id);
416 break;
417 }
418
419 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
420 port_nexus_ptr);
421}
422
Asias He683bd962013-05-06 16:38:27 +0800423static struct se_node_acl *
424tcm_vhost_alloc_fabric_acl(struct se_portal_group *se_tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700425{
426 struct tcm_vhost_nacl *nacl;
427
428 nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL);
429 if (!nacl) {
Masanari Iida744627e92012-11-05 23:30:40 +0900430 pr_err("Unable to allocate struct tcm_vhost_nacl\n");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700431 return NULL;
432 }
433
434 return &nacl->se_node_acl;
435}
436
Asias He683bd962013-05-06 16:38:27 +0800437static void
438tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
439 struct se_node_acl *se_nacl)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700440{
441 struct tcm_vhost_nacl *nacl = container_of(se_nacl,
442 struct tcm_vhost_nacl, se_node_acl);
443 kfree(nacl);
444}
445
446static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg)
447{
448 return 1;
449}
450
451static void tcm_vhost_release_cmd(struct se_cmd *se_cmd)
452{
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700453 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
454 struct tcm_vhost_cmd, tvc_se_cmd);
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700455 struct se_session *se_sess = se_cmd->se_sess;
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700456
457 if (tv_cmd->tvc_sgl_count) {
458 u32 i;
459 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
460 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
461
462 kfree(tv_cmd->tvc_sgl);
463 }
464
465 tcm_vhost_put_inflight(tv_cmd->inflight);
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700466 percpu_ida_free(&se_sess->sess_tag_pool, se_cmd->map_tag);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700467}
468
469static int tcm_vhost_shutdown_session(struct se_session *se_sess)
470{
471 return 0;
472}
473
474static void tcm_vhost_close_session(struct se_session *se_sess)
475{
476 return;
477}
478
479static u32 tcm_vhost_sess_get_index(struct se_session *se_sess)
480{
481 return 0;
482}
483
484static int tcm_vhost_write_pending(struct se_cmd *se_cmd)
485{
486 /* Go ahead and process the write immediately */
487 target_execute_cmd(se_cmd);
488 return 0;
489}
490
491static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd)
492{
493 return 0;
494}
495
496static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl)
497{
498 return;
499}
500
501static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd)
502{
503 return 0;
504}
505
506static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
507{
508 return 0;
509}
510
Asias He3c63f662013-05-06 16:38:29 +0800511static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *cmd)
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700512{
Asias He3c63f662013-05-06 16:38:29 +0800513 struct vhost_scsi *vs = cmd->tvc_vhost;
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700514
Asias He3c63f662013-05-06 16:38:29 +0800515 llist_add(&cmd->tvc_completion_list, &vs->vs_completion_list);
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700516
517 vhost_work_queue(&vs->dev, &vs->vs_completion_work);
518}
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700519
520static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
521{
Asias He3c63f662013-05-06 16:38:29 +0800522 struct tcm_vhost_cmd *cmd = container_of(se_cmd,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700523 struct tcm_vhost_cmd, tvc_se_cmd);
Asias He3c63f662013-05-06 16:38:29 +0800524 vhost_scsi_complete_cmd(cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700525 return 0;
526}
527
528static int tcm_vhost_queue_status(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
Joern Engelb79fafa2013-07-03 11:22:17 -0400536static void tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700537{
Joern Engelb79fafa2013-07-03 11:22:17 -0400538 return;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700539}
540
Asias Hea6c9af82013-04-25 15:35:21 +0800541static void tcm_vhost_free_evt(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
542{
543 vs->vs_events_nr--;
544 kfree(evt);
545}
546
Asias He683bd962013-05-06 16:38:27 +0800547static struct tcm_vhost_evt *
548tcm_vhost_allocate_evt(struct vhost_scsi *vs,
549 u32 event, u32 reason)
Asias Hea6c9af82013-04-25 15:35:21 +0800550{
Asias He3ab2e422013-04-27 11:16:48 +0800551 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Asias Hea6c9af82013-04-25 15:35:21 +0800552 struct tcm_vhost_evt *evt;
553
554 if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) {
555 vs->vs_events_missed = true;
556 return NULL;
557 }
558
559 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
560 if (!evt) {
561 vq_err(vq, "Failed to allocate tcm_vhost_evt\n");
562 vs->vs_events_missed = true;
563 return NULL;
564 }
565
566 evt->event.event = event;
567 evt->event.reason = reason;
568 vs->vs_events_nr++;
569
570 return evt;
571}
572
Asias He3c63f662013-05-06 16:38:29 +0800573static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700574{
Asias He3c63f662013-05-06 16:38:29 +0800575 struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700576
577 /* TODO locking against target/backend threads? */
Nicholas Bellinger6c131d02013-06-06 01:44:48 -0700578 transport_generic_free_cmd(se_cmd, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700579
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700580}
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700581
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700582static int vhost_scsi_check_stop_free(struct se_cmd *se_cmd)
583{
584 return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700585}
586
Asias He683bd962013-05-06 16:38:27 +0800587static void
588tcm_vhost_do_evt_work(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
Asias Hea6c9af82013-04-25 15:35:21 +0800589{
Asias He3ab2e422013-04-27 11:16:48 +0800590 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Asias Hea6c9af82013-04-25 15:35:21 +0800591 struct virtio_scsi_event *event = &evt->event;
592 struct virtio_scsi_event __user *eventp;
593 unsigned out, in;
594 int head, ret;
595
596 if (!vq->private_data) {
597 vs->vs_events_missed = true;
598 return;
599 }
600
601again:
602 vhost_disable_notify(&vs->dev, vq);
603 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
604 ARRAY_SIZE(vq->iov), &out, &in,
605 NULL, NULL);
606 if (head < 0) {
607 vs->vs_events_missed = true;
608 return;
609 }
610 if (head == vq->num) {
611 if (vhost_enable_notify(&vs->dev, vq))
612 goto again;
613 vs->vs_events_missed = true;
614 return;
615 }
616
617 if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) {
618 vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n",
619 vq->iov[out].iov_len);
620 vs->vs_events_missed = true;
621 return;
622 }
623
624 if (vs->vs_events_missed) {
625 event->event |= VIRTIO_SCSI_T_EVENTS_MISSED;
626 vs->vs_events_missed = false;
627 }
628
629 eventp = vq->iov[out].iov_base;
630 ret = __copy_to_user(eventp, event, sizeof(*event));
631 if (!ret)
632 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
633 else
634 vq_err(vq, "Faulted on tcm_vhost_send_event\n");
635}
636
637static void tcm_vhost_evt_work(struct vhost_work *work)
638{
639 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
640 vs_event_work);
Asias He3ab2e422013-04-27 11:16:48 +0800641 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Asias Hea6c9af82013-04-25 15:35:21 +0800642 struct tcm_vhost_evt *evt;
643 struct llist_node *llnode;
644
645 mutex_lock(&vq->mutex);
646 llnode = llist_del_all(&vs->vs_event_list);
647 while (llnode) {
648 evt = llist_entry(llnode, struct tcm_vhost_evt, list);
649 llnode = llist_next(llnode);
650 tcm_vhost_do_evt_work(vs, evt);
651 tcm_vhost_free_evt(vs, evt);
652 }
653 mutex_unlock(&vq->mutex);
654}
655
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700656/* Fill in status and signal that we are done processing this command
657 *
658 * This is scheduled in the vhost work queue so we are called with the owner
659 * process mm and can access the vring.
660 */
661static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
662{
663 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
664 vs_completion_work);
Asias He1b7f3902013-02-06 13:20:59 +0800665 DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
Asias He9d6064a2013-01-06 14:36:13 +0800666 struct virtio_scsi_cmd_resp v_rsp;
Asias He3c63f662013-05-06 16:38:29 +0800667 struct tcm_vhost_cmd *cmd;
Asias He9d6064a2013-01-06 14:36:13 +0800668 struct llist_node *llnode;
669 struct se_cmd *se_cmd;
Asias He1b7f3902013-02-06 13:20:59 +0800670 int ret, vq;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700671
Asias He1b7f3902013-02-06 13:20:59 +0800672 bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
Asias He9d6064a2013-01-06 14:36:13 +0800673 llnode = llist_del_all(&vs->vs_completion_list);
674 while (llnode) {
Asias He3c63f662013-05-06 16:38:29 +0800675 cmd = llist_entry(llnode, struct tcm_vhost_cmd,
Asias He9d6064a2013-01-06 14:36:13 +0800676 tvc_completion_list);
677 llnode = llist_next(llnode);
Asias He3c63f662013-05-06 16:38:29 +0800678 se_cmd = &cmd->tvc_se_cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700679
680 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
Asias He3c63f662013-05-06 16:38:29 +0800681 cmd, se_cmd->residual_count, se_cmd->scsi_status);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700682
683 memset(&v_rsp, 0, sizeof(v_rsp));
684 v_rsp.resid = se_cmd->residual_count;
685 /* TODO is status_qualifier field needed? */
686 v_rsp.status = se_cmd->scsi_status;
687 v_rsp.sense_len = se_cmd->scsi_sense_length;
Asias He3c63f662013-05-06 16:38:29 +0800688 memcpy(v_rsp.sense, cmd->tvc_sense_buf,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700689 v_rsp.sense_len);
Asias He3c63f662013-05-06 16:38:29 +0800690 ret = copy_to_user(cmd->tvc_resp, &v_rsp, sizeof(v_rsp));
Asias He1b7f3902013-02-06 13:20:59 +0800691 if (likely(ret == 0)) {
Asias He3ab2e422013-04-27 11:16:48 +0800692 struct vhost_scsi_virtqueue *q;
Asias He3c63f662013-05-06 16:38:29 +0800693 vhost_add_used(cmd->tvc_vq, cmd->tvc_vq_desc, 0);
694 q = container_of(cmd->tvc_vq, struct vhost_scsi_virtqueue, vq);
Asias He3ab2e422013-04-27 11:16:48 +0800695 vq = q - vs->vqs;
Asias He1b7f3902013-02-06 13:20:59 +0800696 __set_bit(vq, signal);
697 } else
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700698 pr_err("Faulted on virtio_scsi_cmd_resp\n");
699
Asias He3c63f662013-05-06 16:38:29 +0800700 vhost_scsi_free_cmd(cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700701 }
702
Asias He1b7f3902013-02-06 13:20:59 +0800703 vq = -1;
704 while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1))
705 < VHOST_SCSI_MAX_VQ)
Asias He3ab2e422013-04-27 11:16:48 +0800706 vhost_signal(&vs->dev, &vs->vqs[vq].vq);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700707}
708
Asias He683bd962013-05-06 16:38:27 +0800709static struct tcm_vhost_cmd *
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700710vhost_scsi_get_tag(struct vhost_virtqueue *vq,
Asias He98718312013-05-06 16:38:28 +0800711 struct tcm_vhost_tpg *tpg,
Asias He683bd962013-05-06 16:38:27 +0800712 struct virtio_scsi_cmd_req *v_req,
713 u32 exp_data_len,
714 int data_direction)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700715{
Asias He3c63f662013-05-06 16:38:29 +0800716 struct tcm_vhost_cmd *cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700717 struct tcm_vhost_nexus *tv_nexus;
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700718 struct se_session *se_sess;
719 int tag;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700720
Asias He98718312013-05-06 16:38:28 +0800721 tv_nexus = tpg->tpg_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700722 if (!tv_nexus) {
723 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
724 return ERR_PTR(-EIO);
725 }
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700726 se_sess = tv_nexus->tvn_se_sess;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700727
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700728 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, GFP_KERNEL);
729 cmd = &((struct tcm_vhost_cmd *)se_sess->sess_cmd_map)[tag];
730 memset(cmd, 0, sizeof(struct tcm_vhost_cmd));
731
732 cmd->tvc_se_cmd.map_tag = tag;
Asias He3c63f662013-05-06 16:38:29 +0800733 cmd->tvc_tag = v_req->tag;
734 cmd->tvc_task_attr = v_req->task_attr;
735 cmd->tvc_exp_data_len = exp_data_len;
736 cmd->tvc_data_direction = data_direction;
737 cmd->tvc_nexus = tv_nexus;
738 cmd->inflight = tcm_vhost_get_inflight(vq);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700739
Asias He3c63f662013-05-06 16:38:29 +0800740 return cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700741}
742
743/*
744 * Map a user memory range into a scatterlist
745 *
746 * Returns the number of scatterlist entries used or -errno on error.
747 */
Asias He683bd962013-05-06 16:38:27 +0800748static int
749vhost_scsi_map_to_sgl(struct scatterlist *sgl,
750 unsigned int sgl_count,
751 struct iovec *iov,
752 int write)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700753{
Asias He18100532013-01-22 11:20:27 +0800754 unsigned int npages = 0, pages_nr, offset, nbytes;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700755 struct scatterlist *sg = sgl;
Asias He18100532013-01-22 11:20:27 +0800756 void __user *ptr = iov->iov_base;
757 size_t len = iov->iov_len;
758 struct page **pages;
759 int ret, i;
760
761 pages_nr = iov_num_pages(iov);
762 if (pages_nr > sgl_count)
763 return -ENOBUFS;
764
765 pages = kmalloc(pages_nr * sizeof(struct page *), GFP_KERNEL);
766 if (!pages)
767 return -ENOMEM;
768
769 ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages);
770 /* No pages were pinned */
771 if (ret < 0)
772 goto out;
773 /* Less pages pinned than wanted */
774 if (ret != pages_nr) {
775 for (i = 0; i < ret; i++)
776 put_page(pages[i]);
777 ret = -EFAULT;
778 goto out;
779 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700780
781 while (len > 0) {
Asias He18100532013-01-22 11:20:27 +0800782 offset = (uintptr_t)ptr & ~PAGE_MASK;
783 nbytes = min_t(unsigned int, PAGE_SIZE - offset, len);
784 sg_set_page(sg, pages[npages], nbytes, offset);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700785 ptr += nbytes;
786 len -= nbytes;
787 sg++;
788 npages++;
789 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700790
Asias He18100532013-01-22 11:20:27 +0800791out:
792 kfree(pages);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700793 return ret;
794}
795
Asias He683bd962013-05-06 16:38:27 +0800796static int
Asias He3c63f662013-05-06 16:38:29 +0800797vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *cmd,
Asias He683bd962013-05-06 16:38:27 +0800798 struct iovec *iov,
799 unsigned int niov,
800 int write)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700801{
802 int ret;
803 unsigned int i;
804 u32 sgl_count;
805 struct scatterlist *sg;
806
807 /*
808 * Find out how long sglist needs to be
809 */
810 sgl_count = 0;
Asias Hef3158f32013-01-22 11:20:26 +0800811 for (i = 0; i < niov; i++)
812 sgl_count += iov_num_pages(&iov[i]);
813
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700814 /* TODO overflow checking */
815
Asias He3c63f662013-05-06 16:38:29 +0800816 sg = kmalloc(sizeof(cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700817 if (!sg)
818 return -ENOMEM;
Fengguang Wuf0e0e9b2012-07-30 13:19:07 -0700819 pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__,
820 sg, sgl_count, !sg);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700821 sg_init_table(sg, sgl_count);
822
Asias He3c63f662013-05-06 16:38:29 +0800823 cmd->tvc_sgl = sg;
824 cmd->tvc_sgl_count = sgl_count;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700825
826 pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count);
827 for (i = 0; i < niov; i++) {
Asias He18100532013-01-22 11:20:27 +0800828 ret = vhost_scsi_map_to_sgl(sg, sgl_count, &iov[i], write);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700829 if (ret < 0) {
Asias He3c63f662013-05-06 16:38:29 +0800830 for (i = 0; i < cmd->tvc_sgl_count; i++)
831 put_page(sg_page(&cmd->tvc_sgl[i]));
832 kfree(cmd->tvc_sgl);
833 cmd->tvc_sgl = NULL;
834 cmd->tvc_sgl_count = 0;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700835 return ret;
836 }
837
838 sg += ret;
839 sgl_count -= ret;
840 }
841 return 0;
842}
843
844static void tcm_vhost_submission_work(struct work_struct *work)
845{
Asias He3c63f662013-05-06 16:38:29 +0800846 struct tcm_vhost_cmd *cmd =
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700847 container_of(work, struct tcm_vhost_cmd, work);
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700848 struct tcm_vhost_nexus *tv_nexus;
Asias He3c63f662013-05-06 16:38:29 +0800849 struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700850 struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL;
851 int rc, sg_no_bidi = 0;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700852
Asias He3c63f662013-05-06 16:38:29 +0800853 if (cmd->tvc_sgl_count) {
854 sg_ptr = cmd->tvc_sgl;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700855/* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
856#if 0
857 if (se_cmd->se_cmd_flags & SCF_BIDI) {
858 sg_bidi_ptr = NULL;
859 sg_no_bidi = 0;
860 }
861#endif
862 } else {
863 sg_ptr = NULL;
864 }
Asias He3c63f662013-05-06 16:38:29 +0800865 tv_nexus = cmd->tvc_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700866
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700867 rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
Asias He3c63f662013-05-06 16:38:29 +0800868 cmd->tvc_cdb, &cmd->tvc_sense_buf[0],
869 cmd->tvc_lun, cmd->tvc_exp_data_len,
870 cmd->tvc_task_attr, cmd->tvc_data_direction,
Linus Torvalds6d2fa9e2013-07-11 12:57:19 -0700871 TARGET_SCF_ACK_KREF, sg_ptr, cmd->tvc_sgl_count,
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700872 sg_bidi_ptr, sg_no_bidi);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700873 if (rc < 0) {
874 transport_send_check_condition_and_sense(se_cmd,
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700875 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700876 transport_generic_free_cmd(se_cmd, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700877 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700878}
879
Asias He683bd962013-05-06 16:38:27 +0800880static void
881vhost_scsi_send_bad_target(struct vhost_scsi *vs,
882 struct vhost_virtqueue *vq,
883 int head, unsigned out)
Asias He637ab212013-04-10 15:06:15 +0800884{
885 struct virtio_scsi_cmd_resp __user *resp;
886 struct virtio_scsi_cmd_resp rsp;
887 int ret;
888
889 memset(&rsp, 0, sizeof(rsp));
890 rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
891 resp = vq->iov[out].iov_base;
892 ret = __copy_to_user(resp, &rsp, sizeof(rsp));
893 if (!ret)
894 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
895 else
896 pr_err("Faulted on virtio_scsi_cmd_resp\n");
897}
898
Asias He683bd962013-05-06 16:38:27 +0800899static void
900vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700901{
Asias He4f7f46d2013-04-03 14:17:37 +0800902 struct tcm_vhost_tpg **vs_tpg;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700903 struct virtio_scsi_cmd_req v_req;
Asias He98718312013-05-06 16:38:28 +0800904 struct tcm_vhost_tpg *tpg;
Asias He3c63f662013-05-06 16:38:29 +0800905 struct tcm_vhost_cmd *cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700906 u32 exp_data_len, data_first, data_num, data_direction;
907 unsigned out, in, i;
908 int head, ret;
Asias He67e18cf2013-02-05 12:31:57 +0800909 u8 target;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700910
Asias Hee7802212013-05-07 14:54:35 +0800911 mutex_lock(&vq->mutex);
Asias He4f7f46d2013-04-03 14:17:37 +0800912 /*
913 * We can handle the vq only after the endpoint is setup by calling the
914 * VHOST_SCSI_SET_ENDPOINT ioctl.
Asias He4f7f46d2013-04-03 14:17:37 +0800915 */
Asias Hee7802212013-05-07 14:54:35 +0800916 vs_tpg = vq->private_data;
Asias He4f7f46d2013-04-03 14:17:37 +0800917 if (!vs_tpg)
Asias Hee7802212013-05-07 14:54:35 +0800918 goto out;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700919
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700920 vhost_disable_notify(&vs->dev, vq);
921
922 for (;;) {
923 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
924 ARRAY_SIZE(vq->iov), &out, &in,
925 NULL, NULL);
926 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
927 head, out, in);
928 /* On error, stop handling until the next kick. */
929 if (unlikely(head < 0))
930 break;
931 /* Nothing new? Wait for eventfd to tell us they refilled. */
932 if (head == vq->num) {
933 if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
934 vhost_disable_notify(&vs->dev, vq);
935 continue;
936 }
937 break;
938 }
939
940/* FIXME: BIDI operation */
941 if (out == 1 && in == 1) {
942 data_direction = DMA_NONE;
943 data_first = 0;
944 data_num = 0;
945 } else if (out == 1 && in > 1) {
946 data_direction = DMA_FROM_DEVICE;
947 data_first = out + 1;
948 data_num = in - 1;
949 } else if (out > 1 && in == 1) {
950 data_direction = DMA_TO_DEVICE;
951 data_first = 1;
952 data_num = out - 1;
953 } else {
954 vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
955 out, in);
956 break;
957 }
958
959 /*
960 * Check for a sane resp buffer so we can report errors to
961 * the guest.
962 */
963 if (unlikely(vq->iov[out].iov_len !=
964 sizeof(struct virtio_scsi_cmd_resp))) {
965 vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
966 " bytes\n", vq->iov[out].iov_len);
967 break;
968 }
969
970 if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) {
971 vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu"
972 " bytes\n", vq->iov[0].iov_len);
973 break;
974 }
975 pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
976 " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req));
977 ret = __copy_from_user(&v_req, vq->iov[0].iov_base,
978 sizeof(v_req));
979 if (unlikely(ret)) {
980 vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
981 break;
982 }
983
Asias He67e18cf2013-02-05 12:31:57 +0800984 /* Extract the tpgt */
985 target = v_req.lun[1];
Asias He98718312013-05-06 16:38:28 +0800986 tpg = ACCESS_ONCE(vs_tpg[target]);
Asias He67e18cf2013-02-05 12:31:57 +0800987
988 /* Target does not exist, fail the request */
Asias He98718312013-05-06 16:38:28 +0800989 if (unlikely(!tpg)) {
Asias He637ab212013-04-10 15:06:15 +0800990 vhost_scsi_send_bad_target(vs, vq, head, out);
Asias He67e18cf2013-02-05 12:31:57 +0800991 continue;
992 }
993
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700994 exp_data_len = 0;
995 for (i = 0; i < data_num; i++)
996 exp_data_len += vq->iov[data_first + i].iov_len;
997
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700998 cmd = vhost_scsi_get_tag(vq, tpg, &v_req,
999 exp_data_len, data_direction);
Asias He3c63f662013-05-06 16:38:29 +08001000 if (IS_ERR(cmd)) {
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -07001001 vq_err(vq, "vhost_scsi_get_tag failed %ld\n",
Asias He3c63f662013-05-06 16:38:29 +08001002 PTR_ERR(cmd));
Asias He055f6482013-04-10 15:06:16 +08001003 goto err_cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001004 }
1005 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
Asias He3c63f662013-05-06 16:38:29 +08001006 ": %d\n", cmd, exp_data_len, data_direction);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001007
Asias He3c63f662013-05-06 16:38:29 +08001008 cmd->tvc_vhost = vs;
1009 cmd->tvc_vq = vq;
1010 cmd->tvc_resp = vq->iov[out].iov_base;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001011
1012 /*
Asias He3c63f662013-05-06 16:38:29 +08001013 * Copy in the recieved CDB descriptor into cmd->tvc_cdb
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001014 * that will be used by tcm_vhost_new_cmd_map() and down into
1015 * target_setup_cmd_from_cdb()
1016 */
Asias He3c63f662013-05-06 16:38:29 +08001017 memcpy(cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001018 /*
1019 * Check that the recieved CDB size does not exceeded our
1020 * hardcoded max for tcm_vhost
1021 */
1022 /* TODO what if cdb was too small for varlen cdb header? */
Asias He3c63f662013-05-06 16:38:29 +08001023 if (unlikely(scsi_command_size(cmd->tvc_cdb) >
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001024 TCM_VHOST_MAX_CDB_SIZE)) {
1025 vq_err(vq, "Received SCSI CDB with command_size: %d that"
1026 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
Asias He3c63f662013-05-06 16:38:29 +08001027 scsi_command_size(cmd->tvc_cdb),
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001028 TCM_VHOST_MAX_CDB_SIZE);
Asias He055f6482013-04-10 15:06:16 +08001029 goto err_free;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001030 }
Asias He3c63f662013-05-06 16:38:29 +08001031 cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001032
1033 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
Asias He3c63f662013-05-06 16:38:29 +08001034 cmd->tvc_cdb[0], cmd->tvc_lun);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001035
1036 if (data_direction != DMA_NONE) {
Asias He3c63f662013-05-06 16:38:29 +08001037 ret = vhost_scsi_map_iov_to_sgl(cmd,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001038 &vq->iov[data_first], data_num,
1039 data_direction == DMA_TO_DEVICE);
1040 if (unlikely(ret)) {
1041 vq_err(vq, "Failed to map iov to sgl\n");
Asias He055f6482013-04-10 15:06:16 +08001042 goto err_free;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001043 }
1044 }
1045
1046 /*
1047 * Save the descriptor from vhost_get_vq_desc() to be used to
1048 * complete the virtio-scsi request in TCM callback context via
1049 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
1050 */
Asias He3c63f662013-05-06 16:38:29 +08001051 cmd->tvc_vq_desc = head;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001052 /*
1053 * Dispatch tv_cmd descriptor for cmwq execution in process
1054 * context provided by tcm_vhost_workqueue. This also ensures
1055 * tv_cmd is executed on the same kworker CPU as this vhost
1056 * thread to gain positive L2 cache locality effects..
1057 */
Asias He3c63f662013-05-06 16:38:29 +08001058 INIT_WORK(&cmd->work, tcm_vhost_submission_work);
1059 queue_work(tcm_vhost_workqueue, &cmd->work);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001060 }
1061
1062 mutex_unlock(&vq->mutex);
Asias He7ea206c2013-04-10 15:06:14 +08001063 return;
1064
Asias He055f6482013-04-10 15:06:16 +08001065err_free:
Asias He3c63f662013-05-06 16:38:29 +08001066 vhost_scsi_free_cmd(cmd);
Asias He055f6482013-04-10 15:06:16 +08001067err_cmd:
1068 vhost_scsi_send_bad_target(vs, vq, head, out);
Asias Hee7802212013-05-07 14:54:35 +08001069out:
Asias He7ea206c2013-04-10 15:06:14 +08001070 mutex_unlock(&vq->mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001071}
1072
1073static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
1074{
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001075 pr_debug("%s: The handling func for control queue.\n", __func__);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001076}
1077
Asias He683bd962013-05-06 16:38:27 +08001078static void
1079tcm_vhost_send_evt(struct vhost_scsi *vs,
1080 struct tcm_vhost_tpg *tpg,
1081 struct se_lun *lun,
1082 u32 event,
1083 u32 reason)
Asias Hea6c9af82013-04-25 15:35:21 +08001084{
1085 struct tcm_vhost_evt *evt;
1086
1087 evt = tcm_vhost_allocate_evt(vs, event, reason);
1088 if (!evt)
1089 return;
1090
1091 if (tpg && lun) {
1092 /* TODO: share lun setup code with virtio-scsi.ko */
1093 /*
1094 * Note: evt->event is zeroed when we allocate it and
1095 * lun[4-7] need to be zero according to virtio-scsi spec.
1096 */
1097 evt->event.lun[0] = 0x01;
1098 evt->event.lun[1] = tpg->tport_tpgt & 0xFF;
1099 if (lun->unpacked_lun >= 256)
1100 evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
1101 evt->event.lun[3] = lun->unpacked_lun & 0xFF;
1102 }
1103
1104 llist_add(&evt->list, &vs->vs_event_list);
1105 vhost_work_queue(&vs->dev, &vs->vs_event_work);
1106}
1107
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001108static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
1109{
Asias Hea6c9af82013-04-25 15:35:21 +08001110 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1111 poll.work);
1112 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1113
1114 mutex_lock(&vq->mutex);
1115 if (!vq->private_data)
1116 goto out;
1117
1118 if (vs->vs_events_missed)
1119 tcm_vhost_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
1120out:
1121 mutex_unlock(&vq->mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001122}
1123
1124static void vhost_scsi_handle_kick(struct vhost_work *work)
1125{
1126 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1127 poll.work);
1128 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1129
Asias He1b7f3902013-02-06 13:20:59 +08001130 vhost_scsi_handle_vq(vs, vq);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001131}
1132
Asias He4f7f46d2013-04-03 14:17:37 +08001133static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
1134{
Asias He3ab2e422013-04-27 11:16:48 +08001135 vhost_poll_flush(&vs->vqs[index].vq.poll);
Asias He4f7f46d2013-04-03 14:17:37 +08001136}
1137
Michael S. Tsirkin3dfbff32013-04-28 15:38:52 +03001138/* Callers must hold dev mutex */
Asias He4f7f46d2013-04-03 14:17:37 +08001139static void vhost_scsi_flush(struct vhost_scsi *vs)
1140{
Asias Hef2f01732013-04-27 11:16:49 +08001141 struct vhost_scsi_inflight *old_inflight[VHOST_SCSI_MAX_VQ];
Asias He4f7f46d2013-04-03 14:17:37 +08001142 int i;
1143
Asias Hef2f01732013-04-27 11:16:49 +08001144 /* Init new inflight and remember the old inflight */
1145 tcm_vhost_init_inflight(vs, old_inflight);
1146
1147 /*
1148 * The inflight->kref was initialized to 1. We decrement it here to
1149 * indicate the start of the flush operation so that it will reach 0
1150 * when all the reqs are finished.
1151 */
1152 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1153 kref_put(&old_inflight[i]->kref, tcm_vhost_done_inflight);
1154
1155 /* Flush both the vhost poll and vhost work */
Asias He4f7f46d2013-04-03 14:17:37 +08001156 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1157 vhost_scsi_flush_vq(vs, i);
1158 vhost_work_flush(&vs->dev, &vs->vs_completion_work);
Asias Hea6c9af82013-04-25 15:35:21 +08001159 vhost_work_flush(&vs->dev, &vs->vs_event_work);
Asias Hef2f01732013-04-27 11:16:49 +08001160
1161 /* Wait for all reqs issued before the flush to be finished */
1162 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1163 wait_for_completion(&old_inflight[i]->comp);
Asias He4f7f46d2013-04-03 14:17:37 +08001164}
1165
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001166/*
1167 * Called from vhost_scsi_ioctl() context to walk the list of available
1168 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
Asias Hef2b7daf2013-04-25 15:35:20 +08001169 *
1170 * The lock nesting rule is:
1171 * tcm_vhost_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001172 */
Asias He683bd962013-05-06 16:38:27 +08001173static int
1174vhost_scsi_set_endpoint(struct vhost_scsi *vs,
1175 struct vhost_scsi_target *t)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001176{
1177 struct tcm_vhost_tport *tv_tport;
Asias He98718312013-05-06 16:38:28 +08001178 struct tcm_vhost_tpg *tpg;
Asias He4f7f46d2013-04-03 14:17:37 +08001179 struct tcm_vhost_tpg **vs_tpg;
1180 struct vhost_virtqueue *vq;
1181 int index, ret, i, len;
Asias He67e18cf2013-02-05 12:31:57 +08001182 bool match = false;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001183
Asias Hef2b7daf2013-04-25 15:35:20 +08001184 mutex_lock(&tcm_vhost_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001185 mutex_lock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001186
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001187 /* Verify that ring has been setup correctly. */
1188 for (index = 0; index < vs->dev.nvqs; ++index) {
1189 /* Verify that ring has been setup correctly. */
Asias He3ab2e422013-04-27 11:16:48 +08001190 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
Asias Hef2b7daf2013-04-25 15:35:20 +08001191 ret = -EFAULT;
1192 goto out;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001193 }
1194 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001195
Asias He4f7f46d2013-04-03 14:17:37 +08001196 len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
1197 vs_tpg = kzalloc(len, GFP_KERNEL);
1198 if (!vs_tpg) {
Asias Hef2b7daf2013-04-25 15:35:20 +08001199 ret = -ENOMEM;
1200 goto out;
Asias He4f7f46d2013-04-03 14:17:37 +08001201 }
1202 if (vs->vs_tpg)
1203 memcpy(vs_tpg, vs->vs_tpg, len);
1204
Asias He98718312013-05-06 16:38:28 +08001205 list_for_each_entry(tpg, &tcm_vhost_list, tv_tpg_list) {
1206 mutex_lock(&tpg->tv_tpg_mutex);
1207 if (!tpg->tpg_nexus) {
1208 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001209 continue;
1210 }
Asias He98718312013-05-06 16:38:28 +08001211 if (tpg->tv_tpg_vhost_count != 0) {
1212 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001213 continue;
1214 }
Asias He98718312013-05-06 16:38:28 +08001215 tv_tport = tpg->tport;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001216
Asias He67e18cf2013-02-05 12:31:57 +08001217 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
Asias He98718312013-05-06 16:38:28 +08001218 if (vs->vs_tpg && vs->vs_tpg[tpg->tport_tpgt]) {
Asias He4f7f46d2013-04-03 14:17:37 +08001219 kfree(vs_tpg);
Asias He98718312013-05-06 16:38:28 +08001220 mutex_unlock(&tpg->tv_tpg_mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001221 ret = -EEXIST;
1222 goto out;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001223 }
Asias He98718312013-05-06 16:38:28 +08001224 tpg->tv_tpg_vhost_count++;
1225 tpg->vhost_scsi = vs;
1226 vs_tpg[tpg->tport_tpgt] = tpg;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001227 smp_mb__after_atomic_inc();
Asias He67e18cf2013-02-05 12:31:57 +08001228 match = true;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001229 }
Asias He98718312013-05-06 16:38:28 +08001230 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001231 }
Asias He67e18cf2013-02-05 12:31:57 +08001232
1233 if (match) {
1234 memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
1235 sizeof(vs->vs_vhost_wwpn));
Asias He4f7f46d2013-04-03 14:17:37 +08001236 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
Asias He3ab2e422013-04-27 11:16:48 +08001237 vq = &vs->vqs[i].vq;
Asias He4f7f46d2013-04-03 14:17:37 +08001238 mutex_lock(&vq->mutex);
Asias He22fa90c2013-05-07 14:54:36 +08001239 vq->private_data = vs_tpg;
Asias Hedfd5d562013-04-03 14:17:38 +08001240 vhost_init_used(vq);
Asias He4f7f46d2013-04-03 14:17:37 +08001241 mutex_unlock(&vq->mutex);
1242 }
Asias He67e18cf2013-02-05 12:31:57 +08001243 ret = 0;
1244 } else {
1245 ret = -EEXIST;
1246 }
1247
Asias He4f7f46d2013-04-03 14:17:37 +08001248 /*
1249 * Act as synchronize_rcu to make sure access to
1250 * old vs->vs_tpg is finished.
1251 */
1252 vhost_scsi_flush(vs);
1253 kfree(vs->vs_tpg);
1254 vs->vs_tpg = vs_tpg;
1255
Asias Hef2b7daf2013-04-25 15:35:20 +08001256out:
Asias He67e18cf2013-02-05 12:31:57 +08001257 mutex_unlock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001258 mutex_unlock(&tcm_vhost_mutex);
Asias He67e18cf2013-02-05 12:31:57 +08001259 return ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001260}
1261
Asias He683bd962013-05-06 16:38:27 +08001262static int
1263vhost_scsi_clear_endpoint(struct vhost_scsi *vs,
1264 struct vhost_scsi_target *t)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001265{
1266 struct tcm_vhost_tport *tv_tport;
Asias He98718312013-05-06 16:38:28 +08001267 struct tcm_vhost_tpg *tpg;
Asias He4f7f46d2013-04-03 14:17:37 +08001268 struct vhost_virtqueue *vq;
1269 bool match = false;
Asias He67e18cf2013-02-05 12:31:57 +08001270 int index, ret, i;
1271 u8 target;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001272
Asias Hef2b7daf2013-04-25 15:35:20 +08001273 mutex_lock(&tcm_vhost_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001274 mutex_lock(&vs->dev.mutex);
1275 /* Verify that ring has been setup correctly. */
1276 for (index = 0; index < vs->dev.nvqs; ++index) {
Asias He3ab2e422013-04-27 11:16:48 +08001277 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001278 ret = -EFAULT;
Asias He038e0af2013-03-15 09:14:05 +08001279 goto err_dev;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001280 }
1281 }
Asias He4f7f46d2013-04-03 14:17:37 +08001282
1283 if (!vs->vs_tpg) {
Asias Hef2b7daf2013-04-25 15:35:20 +08001284 ret = 0;
1285 goto err_dev;
Asias He4f7f46d2013-04-03 14:17:37 +08001286 }
1287
Asias He67e18cf2013-02-05 12:31:57 +08001288 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
1289 target = i;
Asias He98718312013-05-06 16:38:28 +08001290 tpg = vs->vs_tpg[target];
1291 if (!tpg)
Asias He67e18cf2013-02-05 12:31:57 +08001292 continue;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001293
Asias He98718312013-05-06 16:38:28 +08001294 mutex_lock(&tpg->tv_tpg_mutex);
1295 tv_tport = tpg->tport;
Asias He67e18cf2013-02-05 12:31:57 +08001296 if (!tv_tport) {
1297 ret = -ENODEV;
Asias He038e0af2013-03-15 09:14:05 +08001298 goto err_tpg;
Asias He67e18cf2013-02-05 12:31:57 +08001299 }
1300
1301 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
Asias He98718312013-05-06 16:38:28 +08001302 pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu"
Asias He67e18cf2013-02-05 12:31:57 +08001303 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
Asias He98718312013-05-06 16:38:28 +08001304 tv_tport->tport_name, tpg->tport_tpgt,
Asias He67e18cf2013-02-05 12:31:57 +08001305 t->vhost_wwpn, t->vhost_tpgt);
1306 ret = -EINVAL;
Asias He038e0af2013-03-15 09:14:05 +08001307 goto err_tpg;
Asias He67e18cf2013-02-05 12:31:57 +08001308 }
Asias He98718312013-05-06 16:38:28 +08001309 tpg->tv_tpg_vhost_count--;
1310 tpg->vhost_scsi = NULL;
Asias He67e18cf2013-02-05 12:31:57 +08001311 vs->vs_tpg[target] = NULL;
Asias He4f7f46d2013-04-03 14:17:37 +08001312 match = true;
Asias He98718312013-05-06 16:38:28 +08001313 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001314 }
Asias He4f7f46d2013-04-03 14:17:37 +08001315 if (match) {
1316 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
Asias He3ab2e422013-04-27 11:16:48 +08001317 vq = &vs->vqs[i].vq;
Asias He4f7f46d2013-04-03 14:17:37 +08001318 mutex_lock(&vq->mutex);
Asias He22fa90c2013-05-07 14:54:36 +08001319 vq->private_data = NULL;
Asias He4f7f46d2013-04-03 14:17:37 +08001320 mutex_unlock(&vq->mutex);
1321 }
1322 }
1323 /*
1324 * Act as synchronize_rcu to make sure access to
1325 * old vs->vs_tpg is finished.
1326 */
1327 vhost_scsi_flush(vs);
1328 kfree(vs->vs_tpg);
1329 vs->vs_tpg = NULL;
Asias Hea6c9af82013-04-25 15:35:21 +08001330 WARN_ON(vs->vs_events_nr);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001331 mutex_unlock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001332 mutex_unlock(&tcm_vhost_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001333 return 0;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001334
Asias He038e0af2013-03-15 09:14:05 +08001335err_tpg:
Asias He98718312013-05-06 16:38:28 +08001336 mutex_unlock(&tpg->tv_tpg_mutex);
Asias He038e0af2013-03-15 09:14:05 +08001337err_dev:
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001338 mutex_unlock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001339 mutex_unlock(&tcm_vhost_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001340 return ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001341}
1342
Asias He4f7f46d2013-04-03 14:17:37 +08001343static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
1344{
1345 if (features & ~VHOST_SCSI_FEATURES)
1346 return -EOPNOTSUPP;
1347
1348 mutex_lock(&vs->dev.mutex);
1349 if ((features & (1 << VHOST_F_LOG_ALL)) &&
1350 !vhost_log_access_ok(&vs->dev)) {
1351 mutex_unlock(&vs->dev.mutex);
1352 return -EFAULT;
1353 }
1354 vs->dev.acked_features = features;
1355 smp_wmb();
1356 vhost_scsi_flush(vs);
1357 mutex_unlock(&vs->dev.mutex);
1358 return 0;
1359}
1360
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001361static int vhost_scsi_open(struct inode *inode, struct file *f)
1362{
Asias Hec7289312013-05-06 16:38:26 +08001363 struct vhost_scsi *vs;
Asias He3ab2e422013-04-27 11:16:48 +08001364 struct vhost_virtqueue **vqs;
Asias He1b7f3902013-02-06 13:20:59 +08001365 int r, i;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001366
Asias Hec7289312013-05-06 16:38:26 +08001367 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
1368 if (!vs)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001369 return -ENOMEM;
1370
Asias He3ab2e422013-04-27 11:16:48 +08001371 vqs = kmalloc(VHOST_SCSI_MAX_VQ * sizeof(*vqs), GFP_KERNEL);
1372 if (!vqs) {
Asias Hec7289312013-05-06 16:38:26 +08001373 kfree(vs);
Asias He3ab2e422013-04-27 11:16:48 +08001374 return -ENOMEM;
1375 }
1376
Asias Hec7289312013-05-06 16:38:26 +08001377 vhost_work_init(&vs->vs_completion_work, vhost_scsi_complete_cmd_work);
1378 vhost_work_init(&vs->vs_event_work, tcm_vhost_evt_work);
Asias Hea6c9af82013-04-25 15:35:21 +08001379
Asias Hec7289312013-05-06 16:38:26 +08001380 vs->vs_events_nr = 0;
1381 vs->vs_events_missed = false;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001382
Asias Hec7289312013-05-06 16:38:26 +08001383 vqs[VHOST_SCSI_VQ_CTL] = &vs->vqs[VHOST_SCSI_VQ_CTL].vq;
1384 vqs[VHOST_SCSI_VQ_EVT] = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1385 vs->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick;
1386 vs->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick;
Asias He3ab2e422013-04-27 11:16:48 +08001387 for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) {
Asias Hec7289312013-05-06 16:38:26 +08001388 vqs[i] = &vs->vqs[i].vq;
1389 vs->vqs[i].vq.handle_kick = vhost_scsi_handle_kick;
Asias He3ab2e422013-04-27 11:16:48 +08001390 }
Asias Hec7289312013-05-06 16:38:26 +08001391 r = vhost_dev_init(&vs->dev, vqs, VHOST_SCSI_MAX_VQ);
Asias Hef2f01732013-04-27 11:16:49 +08001392
Asias Hec7289312013-05-06 16:38:26 +08001393 tcm_vhost_init_inflight(vs, NULL);
Asias Hef2f01732013-04-27 11:16:49 +08001394
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001395 if (r < 0) {
Asias He3ab2e422013-04-27 11:16:48 +08001396 kfree(vqs);
Asias Hec7289312013-05-06 16:38:26 +08001397 kfree(vs);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001398 return r;
1399 }
1400
Asias Hec7289312013-05-06 16:38:26 +08001401 f->private_data = vs;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001402 return 0;
1403}
1404
1405static int vhost_scsi_release(struct inode *inode, struct file *f)
1406{
Asias Hec7289312013-05-06 16:38:26 +08001407 struct vhost_scsi *vs = f->private_data;
Asias He67e18cf2013-02-05 12:31:57 +08001408 struct vhost_scsi_target t;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001409
Asias Hec7289312013-05-06 16:38:26 +08001410 mutex_lock(&vs->dev.mutex);
1411 memcpy(t.vhost_wwpn, vs->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
1412 mutex_unlock(&vs->dev.mutex);
1413 vhost_scsi_clear_endpoint(vs, &t);
1414 vhost_dev_stop(&vs->dev);
1415 vhost_dev_cleanup(&vs->dev, false);
Asias Hea6c9af82013-04-25 15:35:21 +08001416 /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
Asias Hec7289312013-05-06 16:38:26 +08001417 vhost_scsi_flush(vs);
1418 kfree(vs->dev.vqs);
1419 kfree(vs);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001420 return 0;
1421}
1422
Asias He683bd962013-05-06 16:38:27 +08001423static long
1424vhost_scsi_ioctl(struct file *f,
1425 unsigned int ioctl,
1426 unsigned long arg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001427{
1428 struct vhost_scsi *vs = f->private_data;
1429 struct vhost_scsi_target backend;
1430 void __user *argp = (void __user *)arg;
1431 u64 __user *featurep = argp;
Asias He11c63412013-04-25 15:35:22 +08001432 u32 __user *eventsp = argp;
1433 u32 events_missed;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001434 u64 features;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001435 int r, abi_version = VHOST_SCSI_ABI_VERSION;
Asias He3ab2e422013-04-27 11:16:48 +08001436 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001437
1438 switch (ioctl) {
1439 case VHOST_SCSI_SET_ENDPOINT:
1440 if (copy_from_user(&backend, argp, sizeof backend))
1441 return -EFAULT;
Michael S. Tsirkin6de71452012-08-18 15:44:09 -07001442 if (backend.reserved != 0)
1443 return -EOPNOTSUPP;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001444
1445 return vhost_scsi_set_endpoint(vs, &backend);
1446 case VHOST_SCSI_CLEAR_ENDPOINT:
1447 if (copy_from_user(&backend, argp, sizeof backend))
1448 return -EFAULT;
Michael S. Tsirkin6de71452012-08-18 15:44:09 -07001449 if (backend.reserved != 0)
1450 return -EOPNOTSUPP;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001451
1452 return vhost_scsi_clear_endpoint(vs, &backend);
1453 case VHOST_SCSI_GET_ABI_VERSION:
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001454 if (copy_to_user(argp, &abi_version, sizeof abi_version))
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001455 return -EFAULT;
1456 return 0;
Asias He11c63412013-04-25 15:35:22 +08001457 case VHOST_SCSI_SET_EVENTS_MISSED:
1458 if (get_user(events_missed, eventsp))
1459 return -EFAULT;
1460 mutex_lock(&vq->mutex);
1461 vs->vs_events_missed = events_missed;
1462 mutex_unlock(&vq->mutex);
1463 return 0;
1464 case VHOST_SCSI_GET_EVENTS_MISSED:
1465 mutex_lock(&vq->mutex);
1466 events_missed = vs->vs_events_missed;
1467 mutex_unlock(&vq->mutex);
1468 if (put_user(events_missed, eventsp))
1469 return -EFAULT;
1470 return 0;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001471 case VHOST_GET_FEATURES:
Nicholas Bellinger5dade712013-03-27 17:23:41 -07001472 features = VHOST_SCSI_FEATURES;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001473 if (copy_to_user(featurep, &features, sizeof features))
1474 return -EFAULT;
1475 return 0;
1476 case VHOST_SET_FEATURES:
1477 if (copy_from_user(&features, featurep, sizeof features))
1478 return -EFAULT;
1479 return vhost_scsi_set_features(vs, features);
1480 default:
1481 mutex_lock(&vs->dev.mutex);
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001482 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
1483 /* TODO: flush backend after dev ioctl. */
1484 if (r == -ENOIOCTLCMD)
1485 r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001486 mutex_unlock(&vs->dev.mutex);
1487 return r;
1488 }
1489}
1490
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001491#ifdef CONFIG_COMPAT
1492static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
1493 unsigned long arg)
1494{
1495 return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1496}
1497#endif
1498
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001499static const struct file_operations vhost_scsi_fops = {
1500 .owner = THIS_MODULE,
1501 .release = vhost_scsi_release,
1502 .unlocked_ioctl = vhost_scsi_ioctl,
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001503#ifdef CONFIG_COMPAT
1504 .compat_ioctl = vhost_scsi_compat_ioctl,
1505#endif
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001506 .open = vhost_scsi_open,
1507 .llseek = noop_llseek,
1508};
1509
1510static struct miscdevice vhost_scsi_misc = {
1511 MISC_DYNAMIC_MINOR,
1512 "vhost-scsi",
1513 &vhost_scsi_fops,
1514};
1515
1516static int __init vhost_scsi_register(void)
1517{
1518 return misc_register(&vhost_scsi_misc);
1519}
1520
1521static int vhost_scsi_deregister(void)
1522{
1523 return misc_deregister(&vhost_scsi_misc);
1524}
1525
1526static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
1527{
1528 switch (tport->tport_proto_id) {
1529 case SCSI_PROTOCOL_SAS:
1530 return "SAS";
1531 case SCSI_PROTOCOL_FCP:
1532 return "FCP";
1533 case SCSI_PROTOCOL_ISCSI:
1534 return "iSCSI";
1535 default:
1536 break;
1537 }
1538
1539 return "Unknown";
1540}
1541
Asias He683bd962013-05-06 16:38:27 +08001542static void
1543tcm_vhost_do_plug(struct tcm_vhost_tpg *tpg,
1544 struct se_lun *lun, bool plug)
Asias Hea6c9af82013-04-25 15:35:21 +08001545{
1546
1547 struct vhost_scsi *vs = tpg->vhost_scsi;
1548 struct vhost_virtqueue *vq;
1549 u32 reason;
1550
1551 if (!vs)
1552 return;
1553
1554 mutex_lock(&vs->dev.mutex);
1555 if (!vhost_has_feature(&vs->dev, VIRTIO_SCSI_F_HOTPLUG)) {
1556 mutex_unlock(&vs->dev.mutex);
1557 return;
1558 }
1559
1560 if (plug)
1561 reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
1562 else
1563 reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
1564
Asias He3ab2e422013-04-27 11:16:48 +08001565 vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Asias Hea6c9af82013-04-25 15:35:21 +08001566 mutex_lock(&vq->mutex);
1567 tcm_vhost_send_evt(vs, tpg, lun,
1568 VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
1569 mutex_unlock(&vq->mutex);
1570 mutex_unlock(&vs->dev.mutex);
1571}
1572
1573static void tcm_vhost_hotplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1574{
1575 tcm_vhost_do_plug(tpg, lun, true);
1576}
1577
1578static void tcm_vhost_hotunplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1579{
1580 tcm_vhost_do_plug(tpg, lun, false);
1581}
1582
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001583static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08001584 struct se_lun *lun)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001585{
Asias He98718312013-05-06 16:38:28 +08001586 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001587 struct tcm_vhost_tpg, se_tpg);
1588
Asias Hea6c9af82013-04-25 15:35:21 +08001589 mutex_lock(&tcm_vhost_mutex);
1590
Asias He98718312013-05-06 16:38:28 +08001591 mutex_lock(&tpg->tv_tpg_mutex);
1592 tpg->tv_tpg_port_count++;
1593 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001594
Asias He98718312013-05-06 16:38:28 +08001595 tcm_vhost_hotplug(tpg, lun);
Asias Hea6c9af82013-04-25 15:35:21 +08001596
1597 mutex_unlock(&tcm_vhost_mutex);
1598
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001599 return 0;
1600}
1601
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001602static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08001603 struct se_lun *lun)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001604{
Asias He98718312013-05-06 16:38:28 +08001605 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001606 struct tcm_vhost_tpg, se_tpg);
1607
Asias Hea6c9af82013-04-25 15:35:21 +08001608 mutex_lock(&tcm_vhost_mutex);
1609
Asias He98718312013-05-06 16:38:28 +08001610 mutex_lock(&tpg->tv_tpg_mutex);
1611 tpg->tv_tpg_port_count--;
1612 mutex_unlock(&tpg->tv_tpg_mutex);
Asias Hea6c9af82013-04-25 15:35:21 +08001613
Asias He98718312013-05-06 16:38:28 +08001614 tcm_vhost_hotunplug(tpg, lun);
Asias Hea6c9af82013-04-25 15:35:21 +08001615
1616 mutex_unlock(&tcm_vhost_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001617}
1618
Asias He683bd962013-05-06 16:38:27 +08001619static struct se_node_acl *
1620tcm_vhost_make_nodeacl(struct se_portal_group *se_tpg,
1621 struct config_group *group,
1622 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001623{
1624 struct se_node_acl *se_nacl, *se_nacl_new;
1625 struct tcm_vhost_nacl *nacl;
1626 u64 wwpn = 0;
1627 u32 nexus_depth;
1628
1629 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1630 return ERR_PTR(-EINVAL); */
1631 se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
1632 if (!se_nacl_new)
1633 return ERR_PTR(-ENOMEM);
1634
1635 nexus_depth = 1;
1636 /*
1637 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1638 * when converting a NodeACL from demo mode -> explict
1639 */
1640 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1641 name, nexus_depth);
1642 if (IS_ERR(se_nacl)) {
1643 tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
1644 return se_nacl;
1645 }
1646 /*
1647 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1648 */
1649 nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
1650 nacl->iport_wwpn = wwpn;
1651
1652 return se_nacl;
1653}
1654
1655static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
1656{
1657 struct tcm_vhost_nacl *nacl = container_of(se_acl,
1658 struct tcm_vhost_nacl, se_node_acl);
1659 core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1660 kfree(nacl);
1661}
1662
Asias He98718312013-05-06 16:38:28 +08001663static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tpg,
Asias He683bd962013-05-06 16:38:27 +08001664 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001665{
1666 struct se_portal_group *se_tpg;
1667 struct tcm_vhost_nexus *tv_nexus;
1668
Asias He98718312013-05-06 16:38:28 +08001669 mutex_lock(&tpg->tv_tpg_mutex);
1670 if (tpg->tpg_nexus) {
1671 mutex_unlock(&tpg->tv_tpg_mutex);
1672 pr_debug("tpg->tpg_nexus already exists\n");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001673 return -EEXIST;
1674 }
Asias He98718312013-05-06 16:38:28 +08001675 se_tpg = &tpg->se_tpg;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001676
1677 tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
1678 if (!tv_nexus) {
Asias He98718312013-05-06 16:38:28 +08001679 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001680 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1681 return -ENOMEM;
1682 }
1683 /*
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -07001684 * Initialize the struct se_session pointer and setup tagpool
1685 * for struct tcm_vhost_cmd descriptors
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001686 */
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -07001687 tv_nexus->tvn_se_sess = transport_init_session_tags(
1688 TCM_VHOST_DEFAULT_TAGS,
1689 sizeof(struct tcm_vhost_cmd));
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001690 if (IS_ERR(tv_nexus->tvn_se_sess)) {
Asias He98718312013-05-06 16:38:28 +08001691 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001692 kfree(tv_nexus);
1693 return -ENOMEM;
1694 }
1695 /*
1696 * Since we are running in 'demo mode' this call with generate a
1697 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1698 * the SCSI Initiator port name of the passed configfs group 'name'.
1699 */
1700 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1701 se_tpg, (unsigned char *)name);
1702 if (!tv_nexus->tvn_se_sess->se_node_acl) {
Asias He98718312013-05-06 16:38:28 +08001703 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001704 pr_debug("core_tpg_check_initiator_node_acl() failed"
1705 " for %s\n", name);
1706 transport_free_session(tv_nexus->tvn_se_sess);
1707 kfree(tv_nexus);
1708 return -ENOMEM;
1709 }
1710 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001711 * Now register the TCM vhost virtual I_T Nexus as active with the
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001712 * call to __transport_register_session()
1713 */
1714 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1715 tv_nexus->tvn_se_sess, tv_nexus);
Asias He98718312013-05-06 16:38:28 +08001716 tpg->tpg_nexus = tv_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001717
Asias He98718312013-05-06 16:38:28 +08001718 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001719 return 0;
1720}
1721
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001722static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001723{
1724 struct se_session *se_sess;
1725 struct tcm_vhost_nexus *tv_nexus;
1726
1727 mutex_lock(&tpg->tv_tpg_mutex);
1728 tv_nexus = tpg->tpg_nexus;
1729 if (!tv_nexus) {
1730 mutex_unlock(&tpg->tv_tpg_mutex);
1731 return -ENODEV;
1732 }
1733
1734 se_sess = tv_nexus->tvn_se_sess;
1735 if (!se_sess) {
1736 mutex_unlock(&tpg->tv_tpg_mutex);
1737 return -ENODEV;
1738 }
1739
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001740 if (tpg->tv_tpg_port_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001741 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001742 pr_err("Unable to remove TCM_vhost I_T Nexus with"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001743 " active TPG port count: %d\n",
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001744 tpg->tv_tpg_port_count);
1745 return -EBUSY;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001746 }
1747
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001748 if (tpg->tv_tpg_vhost_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001749 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001750 pr_err("Unable to remove TCM_vhost I_T Nexus with"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001751 " active TPG vhost count: %d\n",
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001752 tpg->tv_tpg_vhost_count);
1753 return -EBUSY;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001754 }
1755
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001756 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001757 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
1758 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1759 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001760 * Release the SCSI I_T Nexus to the emulated vhost Target Port
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001761 */
1762 transport_deregister_session(tv_nexus->tvn_se_sess);
1763 tpg->tpg_nexus = NULL;
1764 mutex_unlock(&tpg->tv_tpg_mutex);
1765
1766 kfree(tv_nexus);
1767 return 0;
1768}
1769
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001770static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08001771 char *page)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001772{
Asias He98718312013-05-06 16:38:28 +08001773 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001774 struct tcm_vhost_tpg, se_tpg);
1775 struct tcm_vhost_nexus *tv_nexus;
1776 ssize_t ret;
1777
Asias He98718312013-05-06 16:38:28 +08001778 mutex_lock(&tpg->tv_tpg_mutex);
1779 tv_nexus = tpg->tpg_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001780 if (!tv_nexus) {
Asias He98718312013-05-06 16:38:28 +08001781 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001782 return -ENODEV;
1783 }
1784 ret = snprintf(page, PAGE_SIZE, "%s\n",
1785 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
Asias He98718312013-05-06 16:38:28 +08001786 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001787
1788 return ret;
1789}
1790
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001791static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08001792 const char *page,
1793 size_t count)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001794{
Asias He98718312013-05-06 16:38:28 +08001795 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001796 struct tcm_vhost_tpg, se_tpg);
Asias He98718312013-05-06 16:38:28 +08001797 struct tcm_vhost_tport *tport_wwn = tpg->tport;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001798 unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
1799 int ret;
1800 /*
1801 * Shutdown the active I_T nexus if 'NULL' is passed..
1802 */
1803 if (!strncmp(page, "NULL", 4)) {
Asias He98718312013-05-06 16:38:28 +08001804 ret = tcm_vhost_drop_nexus(tpg);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001805 return (!ret) ? count : ret;
1806 }
1807 /*
1808 * Otherwise make sure the passed virtual Initiator port WWN matches
1809 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
1810 * tcm_vhost_make_nexus().
1811 */
1812 if (strlen(page) >= TCM_VHOST_NAMELEN) {
1813 pr_err("Emulated NAA Sas Address: %s, exceeds"
1814 " max: %d\n", page, TCM_VHOST_NAMELEN);
1815 return -EINVAL;
1816 }
1817 snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
1818
1819 ptr = strstr(i_port, "naa.");
1820 if (ptr) {
1821 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1822 pr_err("Passed SAS Initiator Port %s does not"
1823 " match target port protoid: %s\n", i_port,
1824 tcm_vhost_dump_proto_id(tport_wwn));
1825 return -EINVAL;
1826 }
1827 port_ptr = &i_port[0];
1828 goto check_newline;
1829 }
1830 ptr = strstr(i_port, "fc.");
1831 if (ptr) {
1832 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1833 pr_err("Passed FCP Initiator Port %s does not"
1834 " match target port protoid: %s\n", i_port,
1835 tcm_vhost_dump_proto_id(tport_wwn));
1836 return -EINVAL;
1837 }
1838 port_ptr = &i_port[3]; /* Skip over "fc." */
1839 goto check_newline;
1840 }
1841 ptr = strstr(i_port, "iqn.");
1842 if (ptr) {
1843 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1844 pr_err("Passed iSCSI Initiator Port %s does not"
1845 " match target port protoid: %s\n", i_port,
1846 tcm_vhost_dump_proto_id(tport_wwn));
1847 return -EINVAL;
1848 }
1849 port_ptr = &i_port[0];
1850 goto check_newline;
1851 }
1852 pr_err("Unable to locate prefix for emulated Initiator Port:"
1853 " %s\n", i_port);
1854 return -EINVAL;
1855 /*
1856 * Clear any trailing newline for the NAA WWN
1857 */
1858check_newline:
1859 if (i_port[strlen(i_port)-1] == '\n')
1860 i_port[strlen(i_port)-1] = '\0';
1861
Asias He98718312013-05-06 16:38:28 +08001862 ret = tcm_vhost_make_nexus(tpg, port_ptr);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001863 if (ret < 0)
1864 return ret;
1865
1866 return count;
1867}
1868
1869TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
1870
1871static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
1872 &tcm_vhost_tpg_nexus.attr,
1873 NULL,
1874};
1875
Asias He683bd962013-05-06 16:38:27 +08001876static struct se_portal_group *
1877tcm_vhost_make_tpg(struct se_wwn *wwn,
1878 struct config_group *group,
1879 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001880{
1881 struct tcm_vhost_tport *tport = container_of(wwn,
1882 struct tcm_vhost_tport, tport_wwn);
1883
1884 struct tcm_vhost_tpg *tpg;
1885 unsigned long tpgt;
1886 int ret;
1887
1888 if (strstr(name, "tpgt_") != name)
1889 return ERR_PTR(-EINVAL);
1890 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
1891 return ERR_PTR(-EINVAL);
1892
1893 tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
1894 if (!tpg) {
1895 pr_err("Unable to allocate struct tcm_vhost_tpg");
1896 return ERR_PTR(-ENOMEM);
1897 }
1898 mutex_init(&tpg->tv_tpg_mutex);
1899 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1900 tpg->tport = tport;
1901 tpg->tport_tpgt = tpgt;
1902
1903 ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
1904 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1905 if (ret < 0) {
1906 kfree(tpg);
1907 return NULL;
1908 }
1909 mutex_lock(&tcm_vhost_mutex);
1910 list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
1911 mutex_unlock(&tcm_vhost_mutex);
1912
1913 return &tpg->se_tpg;
1914}
1915
1916static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
1917{
1918 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1919 struct tcm_vhost_tpg, se_tpg);
1920
1921 mutex_lock(&tcm_vhost_mutex);
1922 list_del(&tpg->tv_tpg_list);
1923 mutex_unlock(&tcm_vhost_mutex);
1924 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001925 * Release the virtual I_T Nexus for this vhost TPG
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001926 */
1927 tcm_vhost_drop_nexus(tpg);
1928 /*
1929 * Deregister the se_tpg from TCM..
1930 */
1931 core_tpg_deregister(se_tpg);
1932 kfree(tpg);
1933}
1934
Asias He683bd962013-05-06 16:38:27 +08001935static struct se_wwn *
1936tcm_vhost_make_tport(struct target_fabric_configfs *tf,
1937 struct config_group *group,
1938 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001939{
1940 struct tcm_vhost_tport *tport;
1941 char *ptr;
1942 u64 wwpn = 0;
1943 int off = 0;
1944
1945 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1946 return ERR_PTR(-EINVAL); */
1947
1948 tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
1949 if (!tport) {
1950 pr_err("Unable to allocate struct tcm_vhost_tport");
1951 return ERR_PTR(-ENOMEM);
1952 }
1953 tport->tport_wwpn = wwpn;
1954 /*
1955 * Determine the emulated Protocol Identifier and Target Port Name
1956 * based on the incoming configfs directory name.
1957 */
1958 ptr = strstr(name, "naa.");
1959 if (ptr) {
1960 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1961 goto check_len;
1962 }
1963 ptr = strstr(name, "fc.");
1964 if (ptr) {
1965 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1966 off = 3; /* Skip over "fc." */
1967 goto check_len;
1968 }
1969 ptr = strstr(name, "iqn.");
1970 if (ptr) {
1971 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1972 goto check_len;
1973 }
1974
1975 pr_err("Unable to locate prefix for emulated Target Port:"
1976 " %s\n", name);
1977 kfree(tport);
1978 return ERR_PTR(-EINVAL);
1979
1980check_len:
1981 if (strlen(name) >= TCM_VHOST_NAMELEN) {
1982 pr_err("Emulated %s Address: %s, exceeds"
1983 " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
1984 TCM_VHOST_NAMELEN);
1985 kfree(tport);
1986 return ERR_PTR(-EINVAL);
1987 }
1988 snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
1989
1990 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
1991 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
1992
1993 return &tport->tport_wwn;
1994}
1995
1996static void tcm_vhost_drop_tport(struct se_wwn *wwn)
1997{
1998 struct tcm_vhost_tport *tport = container_of(wwn,
1999 struct tcm_vhost_tport, tport_wwn);
2000
2001 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
2002 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
2003 tport->tport_name);
2004
2005 kfree(tport);
2006}
2007
Asias He683bd962013-05-06 16:38:27 +08002008static ssize_t
2009tcm_vhost_wwn_show_attr_version(struct target_fabric_configfs *tf,
2010 char *page)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002011{
2012 return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
2013 "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
2014 utsname()->machine);
2015}
2016
2017TF_WWN_ATTR_RO(tcm_vhost, version);
2018
2019static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
2020 &tcm_vhost_wwn_version.attr,
2021 NULL,
2022};
2023
2024static struct target_core_fabric_ops tcm_vhost_ops = {
2025 .get_fabric_name = tcm_vhost_get_fabric_name,
2026 .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
2027 .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
2028 .tpg_get_tag = tcm_vhost_get_tag,
2029 .tpg_get_default_depth = tcm_vhost_get_default_depth,
2030 .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
2031 .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
2032 .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
2033 .tpg_check_demo_mode = tcm_vhost_check_true,
2034 .tpg_check_demo_mode_cache = tcm_vhost_check_true,
2035 .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
2036 .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
2037 .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
2038 .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
2039 .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
2040 .release_cmd = tcm_vhost_release_cmd,
Nicholas Bellinger084ed452013-06-06 02:20:41 -07002041 .check_stop_free = vhost_scsi_check_stop_free,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002042 .shutdown_session = tcm_vhost_shutdown_session,
2043 .close_session = tcm_vhost_close_session,
2044 .sess_get_index = tcm_vhost_sess_get_index,
2045 .sess_get_initiator_sid = NULL,
2046 .write_pending = tcm_vhost_write_pending,
2047 .write_pending_status = tcm_vhost_write_pending_status,
2048 .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
2049 .get_task_tag = tcm_vhost_get_task_tag,
2050 .get_cmd_state = tcm_vhost_get_cmd_state,
2051 .queue_data_in = tcm_vhost_queue_data_in,
2052 .queue_status = tcm_vhost_queue_status,
2053 .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002054 /*
2055 * Setup callers for generic logic in target_core_fabric_configfs.c
2056 */
2057 .fabric_make_wwn = tcm_vhost_make_tport,
2058 .fabric_drop_wwn = tcm_vhost_drop_tport,
2059 .fabric_make_tpg = tcm_vhost_make_tpg,
2060 .fabric_drop_tpg = tcm_vhost_drop_tpg,
2061 .fabric_post_link = tcm_vhost_port_link,
2062 .fabric_pre_unlink = tcm_vhost_port_unlink,
2063 .fabric_make_np = NULL,
2064 .fabric_drop_np = NULL,
2065 .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
2066 .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
2067};
2068
2069static int tcm_vhost_register_configfs(void)
2070{
2071 struct target_fabric_configfs *fabric;
2072 int ret;
2073
2074 pr_debug("TCM_VHOST fabric module %s on %s/%s"
2075 " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
2076 utsname()->machine);
2077 /*
2078 * Register the top level struct config_item_type with TCM core
2079 */
2080 fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
2081 if (IS_ERR(fabric)) {
2082 pr_err("target_fabric_configfs_init() failed\n");
2083 return PTR_ERR(fabric);
2084 }
2085 /*
2086 * Setup fabric->tf_ops from our local tcm_vhost_ops
2087 */
2088 fabric->tf_ops = tcm_vhost_ops;
2089 /*
2090 * Setup default attribute lists for various fabric->tf_cit_tmpl
2091 */
2092 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
2093 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
2094 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
2095 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
2096 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
2097 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
2098 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
2099 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
2100 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
2101 /*
2102 * Register the fabric for use within TCM
2103 */
2104 ret = target_fabric_configfs_register(fabric);
2105 if (ret < 0) {
2106 pr_err("target_fabric_configfs_register() failed"
2107 " for TCM_VHOST\n");
2108 return ret;
2109 }
2110 /*
2111 * Setup our local pointer to *fabric
2112 */
2113 tcm_vhost_fabric_configfs = fabric;
2114 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
2115 return 0;
2116};
2117
2118static void tcm_vhost_deregister_configfs(void)
2119{
2120 if (!tcm_vhost_fabric_configfs)
2121 return;
2122
2123 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
2124 tcm_vhost_fabric_configfs = NULL;
2125 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
2126};
2127
2128static int __init tcm_vhost_init(void)
2129{
2130 int ret = -ENOMEM;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002131 /*
2132 * Use our own dedicated workqueue for submitting I/O into
2133 * target core to avoid contention within system_wq.
2134 */
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002135 tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
2136 if (!tcm_vhost_workqueue)
2137 goto out;
2138
2139 ret = vhost_scsi_register();
2140 if (ret < 0)
2141 goto out_destroy_workqueue;
2142
2143 ret = tcm_vhost_register_configfs();
2144 if (ret < 0)
2145 goto out_vhost_scsi_deregister;
2146
2147 return 0;
2148
2149out_vhost_scsi_deregister:
2150 vhost_scsi_deregister();
2151out_destroy_workqueue:
2152 destroy_workqueue(tcm_vhost_workqueue);
2153out:
2154 return ret;
2155};
2156
2157static void tcm_vhost_exit(void)
2158{
2159 tcm_vhost_deregister_configfs();
2160 vhost_scsi_deregister();
2161 destroy_workqueue(tcm_vhost_workqueue);
2162};
2163
Michael S. Tsirkin181c04a2013-05-02 03:52:59 +03002164MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
2165MODULE_ALIAS("tcm_vhost");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002166MODULE_LICENSE("GPL");
2167module_init(tcm_vhost_init);
2168module_exit(tcm_vhost_exit);