blob: 4264840ef7dcde2b0ed3fa15f4f5611e085fee62 [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 Bellinger057cbf42012-07-18 14:31:32 -070051
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070052#include "vhost.h"
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +030053
54#define TCM_VHOST_VERSION "v0.1"
55#define TCM_VHOST_NAMELEN 256
56#define TCM_VHOST_MAX_CDB_SIZE 32
57
58struct vhost_scsi_inflight {
59 /* Wait for the flush operation to finish */
60 struct completion comp;
61 /* Refcount for the inflight reqs */
62 struct kref kref;
63};
64
65struct tcm_vhost_cmd {
66 /* Descriptor from vhost_get_vq_desc() for virt_queue segment */
67 int tvc_vq_desc;
68 /* virtio-scsi initiator task attribute */
69 int tvc_task_attr;
70 /* virtio-scsi initiator data direction */
71 enum dma_data_direction tvc_data_direction;
72 /* Expected data transfer length from virtio-scsi header */
73 u32 tvc_exp_data_len;
74 /* The Tag from include/linux/virtio_scsi.h:struct virtio_scsi_cmd_req */
75 u64 tvc_tag;
76 /* The number of scatterlists associated with this cmd */
77 u32 tvc_sgl_count;
78 /* Saved unpacked SCSI LUN for tcm_vhost_submission_work() */
79 u32 tvc_lun;
80 /* Pointer to the SGL formatted memory from virtio-scsi */
81 struct scatterlist *tvc_sgl;
82 /* Pointer to response */
83 struct virtio_scsi_cmd_resp __user *tvc_resp;
84 /* Pointer to vhost_scsi for our device */
85 struct vhost_scsi *tvc_vhost;
86 /* Pointer to vhost_virtqueue for the cmd */
87 struct vhost_virtqueue *tvc_vq;
88 /* Pointer to vhost nexus memory */
89 struct tcm_vhost_nexus *tvc_nexus;
90 /* The TCM I/O descriptor that is accessed via container_of() */
91 struct se_cmd tvc_se_cmd;
92 /* work item used for cmwq dispatch to tcm_vhost_submission_work() */
93 struct work_struct work;
94 /* Copy of the incoming SCSI command descriptor block (CDB) */
95 unsigned char tvc_cdb[TCM_VHOST_MAX_CDB_SIZE];
96 /* Sense buffer that will be mapped into outgoing status */
97 unsigned char tvc_sense_buf[TRANSPORT_SENSE_BUFFER];
98 /* Completed commands list, serviced from vhost worker thread */
99 struct llist_node tvc_completion_list;
100 /* Used to track inflight cmd */
101 struct vhost_scsi_inflight *inflight;
102};
103
104struct tcm_vhost_nexus {
105 /* Pointer to TCM session for I_T Nexus */
106 struct se_session *tvn_se_sess;
107};
108
109struct tcm_vhost_nacl {
110 /* Binary World Wide unique Port Name for Vhost Initiator port */
111 u64 iport_wwpn;
112 /* ASCII formatted WWPN for Sas Initiator port */
113 char iport_name[TCM_VHOST_NAMELEN];
114 /* Returned by tcm_vhost_make_nodeacl() */
115 struct se_node_acl se_node_acl;
116};
117
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300118struct tcm_vhost_tpg {
119 /* Vhost port target portal group tag for TCM */
120 u16 tport_tpgt;
121 /* Used to track number of TPG Port/Lun Links wrt to explict I_T Nexus shutdown */
122 int tv_tpg_port_count;
123 /* Used for vhost_scsi device reference to tpg_nexus, protected by tv_tpg_mutex */
124 int tv_tpg_vhost_count;
125 /* list for tcm_vhost_list */
126 struct list_head tv_tpg_list;
127 /* Used to protect access for tpg_nexus */
128 struct mutex tv_tpg_mutex;
129 /* Pointer to the TCM VHost I_T Nexus for this TPG endpoint */
130 struct tcm_vhost_nexus *tpg_nexus;
131 /* Pointer back to tcm_vhost_tport */
132 struct tcm_vhost_tport *tport;
133 /* Returned by tcm_vhost_make_tpg() */
134 struct se_portal_group se_tpg;
135 /* Pointer back to vhost_scsi, protected by tv_tpg_mutex */
136 struct vhost_scsi *vhost_scsi;
137};
138
139struct tcm_vhost_tport {
140 /* SCSI protocol the tport is providing */
141 u8 tport_proto_id;
142 /* Binary World Wide unique Port Name for Vhost Target port */
143 u64 tport_wwpn;
144 /* ASCII formatted WWPN for Vhost Target port */
145 char tport_name[TCM_VHOST_NAMELEN];
146 /* Returned by tcm_vhost_make_tport() */
147 struct se_wwn tport_wwn;
148};
149
150struct tcm_vhost_evt {
151 /* event to be sent to guest */
152 struct virtio_scsi_event event;
153 /* event list, serviced from vhost worker thread */
154 struct llist_node list;
155};
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700156
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700157enum {
158 VHOST_SCSI_VQ_CTL = 0,
159 VHOST_SCSI_VQ_EVT = 1,
160 VHOST_SCSI_VQ_IO = 2,
161};
162
Nicholas Bellinger5dade712013-03-27 17:23:41 -0700163enum {
Asias Hea18cc422013-05-07 14:51:49 +0800164 VHOST_SCSI_FEATURES = VHOST_FEATURES | (1ULL << VIRTIO_SCSI_F_HOTPLUG)
Nicholas Bellinger5dade712013-03-27 17:23:41 -0700165};
166
Asias He1b7f3902013-02-06 13:20:59 +0800167#define VHOST_SCSI_MAX_TARGET 256
168#define VHOST_SCSI_MAX_VQ 128
Asias Hea6c9af82013-04-25 15:35:21 +0800169#define VHOST_SCSI_MAX_EVENT 128
Asias He67e18cf2013-02-05 12:31:57 +0800170
Asias He3ab2e422013-04-27 11:16:48 +0800171struct vhost_scsi_virtqueue {
172 struct vhost_virtqueue vq;
Michael S. Tsirkin3dfbff32013-04-28 15:38:52 +0300173 /*
174 * Reference counting for inflight reqs, used for flush operation. At
175 * each time, one reference tracks new commands submitted, while we
176 * wait for another one to reach 0.
177 */
Asias Hef2f0173d2013-04-27 11:16:49 +0800178 struct vhost_scsi_inflight inflights[2];
Michael S. Tsirkin3dfbff32013-04-28 15:38:52 +0300179 /*
180 * Indicate current inflight in use, protected by vq->mutex.
181 * Writers must also take dev mutex and flush under it.
182 */
Asias Hef2f0173d2013-04-27 11:16:49 +0800183 int inflight_idx;
Asias He3ab2e422013-04-27 11:16:48 +0800184};
185
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700186struct vhost_scsi {
Asias He67e18cf2013-02-05 12:31:57 +0800187 /* Protected by vhost_scsi->dev.mutex */
Asias He4f7f46d2013-04-03 14:17:37 +0800188 struct tcm_vhost_tpg **vs_tpg;
Asias He67e18cf2013-02-05 12:31:57 +0800189 char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
Asias He67e18cf2013-02-05 12:31:57 +0800190
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700191 struct vhost_dev dev;
Asias He3ab2e422013-04-27 11:16:48 +0800192 struct vhost_scsi_virtqueue vqs[VHOST_SCSI_MAX_VQ];
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700193
194 struct vhost_work vs_completion_work; /* cmd completion work item */
Asias He9d6064a2013-01-06 14:36:13 +0800195 struct llist_head vs_completion_list; /* cmd completion queue */
Asias Hea6c9af82013-04-25 15:35:21 +0800196
197 struct vhost_work vs_event_work; /* evt injection work item */
198 struct llist_head vs_event_list; /* evt injection queue */
199
200 bool vs_events_missed; /* any missed events, protected by vq->mutex */
201 int vs_events_nr; /* num of pending events, protected by vq->mutex */
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700202};
203
204/* Local pointer to allocated TCM configfs fabric module */
205static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
206
207static struct workqueue_struct *tcm_vhost_workqueue;
208
209/* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
210static DEFINE_MUTEX(tcm_vhost_mutex);
211static LIST_HEAD(tcm_vhost_list);
212
Asias He765b34f2013-01-22 11:20:25 +0800213static int iov_num_pages(struct iovec *iov)
214{
215 return (PAGE_ALIGN((unsigned long)iov->iov_base + iov->iov_len) -
216 ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
217}
218
Asias He0a1febf2013-06-05 21:17:38 +0800219static void tcm_vhost_done_inflight(struct kref *kref)
Asias Hef2f0173d2013-04-27 11:16:49 +0800220{
221 struct vhost_scsi_inflight *inflight;
222
223 inflight = container_of(kref, struct vhost_scsi_inflight, kref);
224 complete(&inflight->comp);
225}
226
227static void tcm_vhost_init_inflight(struct vhost_scsi *vs,
228 struct vhost_scsi_inflight *old_inflight[])
229{
230 struct vhost_scsi_inflight *new_inflight;
231 struct vhost_virtqueue *vq;
232 int idx, i;
233
234 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
235 vq = &vs->vqs[i].vq;
236
237 mutex_lock(&vq->mutex);
238
239 /* store old infight */
240 idx = vs->vqs[i].inflight_idx;
241 if (old_inflight)
242 old_inflight[i] = &vs->vqs[i].inflights[idx];
243
244 /* setup new infight */
245 vs->vqs[i].inflight_idx = idx ^ 1;
246 new_inflight = &vs->vqs[i].inflights[idx ^ 1];
247 kref_init(&new_inflight->kref);
248 init_completion(&new_inflight->comp);
249
250 mutex_unlock(&vq->mutex);
251 }
252}
253
254static struct vhost_scsi_inflight *
255tcm_vhost_get_inflight(struct vhost_virtqueue *vq)
256{
257 struct vhost_scsi_inflight *inflight;
258 struct vhost_scsi_virtqueue *svq;
259
260 svq = container_of(vq, struct vhost_scsi_virtqueue, vq);
261 inflight = &svq->inflights[svq->inflight_idx];
262 kref_get(&inflight->kref);
263
264 return inflight;
265}
266
267static void tcm_vhost_put_inflight(struct vhost_scsi_inflight *inflight)
268{
269 kref_put(&inflight->kref, tcm_vhost_done_inflight);
270}
271
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700272static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
273{
274 return 1;
275}
276
277static int tcm_vhost_check_false(struct se_portal_group *se_tpg)
278{
279 return 0;
280}
281
282static char *tcm_vhost_get_fabric_name(void)
283{
284 return "vhost";
285}
286
287static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg)
288{
289 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
290 struct tcm_vhost_tpg, se_tpg);
291 struct tcm_vhost_tport *tport = tpg->tport;
292
293 switch (tport->tport_proto_id) {
294 case SCSI_PROTOCOL_SAS:
295 return sas_get_fabric_proto_ident(se_tpg);
296 case SCSI_PROTOCOL_FCP:
297 return fc_get_fabric_proto_ident(se_tpg);
298 case SCSI_PROTOCOL_ISCSI:
299 return iscsi_get_fabric_proto_ident(se_tpg);
300 default:
301 pr_err("Unknown tport_proto_id: 0x%02x, using"
302 " SAS emulation\n", tport->tport_proto_id);
303 break;
304 }
305
306 return sas_get_fabric_proto_ident(se_tpg);
307}
308
309static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg)
310{
311 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
312 struct tcm_vhost_tpg, se_tpg);
313 struct tcm_vhost_tport *tport = tpg->tport;
314
315 return &tport->tport_name[0];
316}
317
318static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg)
319{
320 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
321 struct tcm_vhost_tpg, se_tpg);
322 return tpg->tport_tpgt;
323}
324
325static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
326{
327 return 1;
328}
329
Asias He683bd962013-05-06 16:38:27 +0800330static u32
331tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
332 struct se_node_acl *se_nacl,
333 struct t10_pr_registration *pr_reg,
334 int *format_code,
335 unsigned char *buf)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700336{
337 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
338 struct tcm_vhost_tpg, se_tpg);
339 struct tcm_vhost_tport *tport = tpg->tport;
340
341 switch (tport->tport_proto_id) {
342 case SCSI_PROTOCOL_SAS:
343 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
344 format_code, buf);
345 case SCSI_PROTOCOL_FCP:
346 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
347 format_code, buf);
348 case SCSI_PROTOCOL_ISCSI:
349 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
350 format_code, buf);
351 default:
352 pr_err("Unknown tport_proto_id: 0x%02x, using"
353 " SAS emulation\n", tport->tport_proto_id);
354 break;
355 }
356
357 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
358 format_code, buf);
359}
360
Asias He683bd962013-05-06 16:38:27 +0800361static u32
362tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
363 struct se_node_acl *se_nacl,
364 struct t10_pr_registration *pr_reg,
365 int *format_code)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700366{
367 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
368 struct tcm_vhost_tpg, se_tpg);
369 struct tcm_vhost_tport *tport = tpg->tport;
370
371 switch (tport->tport_proto_id) {
372 case SCSI_PROTOCOL_SAS:
373 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
374 format_code);
375 case SCSI_PROTOCOL_FCP:
376 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
377 format_code);
378 case SCSI_PROTOCOL_ISCSI:
379 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
380 format_code);
381 default:
382 pr_err("Unknown tport_proto_id: 0x%02x, using"
383 " SAS emulation\n", tport->tport_proto_id);
384 break;
385 }
386
387 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
388 format_code);
389}
390
Asias He683bd962013-05-06 16:38:27 +0800391static char *
392tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
393 const char *buf,
394 u32 *out_tid_len,
395 char **port_nexus_ptr)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700396{
397 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
398 struct tcm_vhost_tpg, se_tpg);
399 struct tcm_vhost_tport *tport = tpg->tport;
400
401 switch (tport->tport_proto_id) {
402 case SCSI_PROTOCOL_SAS:
403 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
404 port_nexus_ptr);
405 case SCSI_PROTOCOL_FCP:
406 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
407 port_nexus_ptr);
408 case SCSI_PROTOCOL_ISCSI:
409 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
410 port_nexus_ptr);
411 default:
412 pr_err("Unknown tport_proto_id: 0x%02x, using"
413 " SAS emulation\n", tport->tport_proto_id);
414 break;
415 }
416
417 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
418 port_nexus_ptr);
419}
420
Asias He683bd962013-05-06 16:38:27 +0800421static struct se_node_acl *
422tcm_vhost_alloc_fabric_acl(struct se_portal_group *se_tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700423{
424 struct tcm_vhost_nacl *nacl;
425
426 nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL);
427 if (!nacl) {
Masanari Iida744627e92012-11-05 23:30:40 +0900428 pr_err("Unable to allocate struct tcm_vhost_nacl\n");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700429 return NULL;
430 }
431
432 return &nacl->se_node_acl;
433}
434
Asias He683bd962013-05-06 16:38:27 +0800435static void
436tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
437 struct se_node_acl *se_nacl)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700438{
439 struct tcm_vhost_nacl *nacl = container_of(se_nacl,
440 struct tcm_vhost_nacl, se_node_acl);
441 kfree(nacl);
442}
443
444static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg)
445{
446 return 1;
447}
448
449static void tcm_vhost_release_cmd(struct se_cmd *se_cmd)
450{
451 return;
452}
453
454static int tcm_vhost_shutdown_session(struct se_session *se_sess)
455{
456 return 0;
457}
458
459static void tcm_vhost_close_session(struct se_session *se_sess)
460{
461 return;
462}
463
464static u32 tcm_vhost_sess_get_index(struct se_session *se_sess)
465{
466 return 0;
467}
468
469static int tcm_vhost_write_pending(struct se_cmd *se_cmd)
470{
471 /* Go ahead and process the write immediately */
472 target_execute_cmd(se_cmd);
473 return 0;
474}
475
476static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd)
477{
478 return 0;
479}
480
481static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl)
482{
483 return;
484}
485
486static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd)
487{
488 return 0;
489}
490
491static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
492{
493 return 0;
494}
495
Asias He3c63f662013-05-06 16:38:29 +0800496static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *cmd)
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700497{
Asias He3c63f662013-05-06 16:38:29 +0800498 struct vhost_scsi *vs = cmd->tvc_vhost;
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700499
Asias He3c63f662013-05-06 16:38:29 +0800500 llist_add(&cmd->tvc_completion_list, &vs->vs_completion_list);
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700501
502 vhost_work_queue(&vs->dev, &vs->vs_completion_work);
503}
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700504
505static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
506{
Asias He3c63f662013-05-06 16:38:29 +0800507 struct tcm_vhost_cmd *cmd = container_of(se_cmd,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700508 struct tcm_vhost_cmd, tvc_se_cmd);
Asias He3c63f662013-05-06 16:38:29 +0800509 vhost_scsi_complete_cmd(cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700510 return 0;
511}
512
513static int tcm_vhost_queue_status(struct se_cmd *se_cmd)
514{
Asias He3c63f662013-05-06 16:38:29 +0800515 struct tcm_vhost_cmd *cmd = container_of(se_cmd,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700516 struct tcm_vhost_cmd, tvc_se_cmd);
Asias He3c63f662013-05-06 16:38:29 +0800517 vhost_scsi_complete_cmd(cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700518 return 0;
519}
520
521static int tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
522{
523 return 0;
524}
525
Asias Hea6c9af82013-04-25 15:35:21 +0800526static void tcm_vhost_free_evt(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
527{
528 vs->vs_events_nr--;
529 kfree(evt);
530}
531
Asias He683bd962013-05-06 16:38:27 +0800532static struct tcm_vhost_evt *
533tcm_vhost_allocate_evt(struct vhost_scsi *vs,
534 u32 event, u32 reason)
Asias Hea6c9af82013-04-25 15:35:21 +0800535{
Asias He3ab2e422013-04-27 11:16:48 +0800536 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Asias Hea6c9af82013-04-25 15:35:21 +0800537 struct tcm_vhost_evt *evt;
538
539 if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) {
540 vs->vs_events_missed = true;
541 return NULL;
542 }
543
544 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
545 if (!evt) {
546 vq_err(vq, "Failed to allocate tcm_vhost_evt\n");
547 vs->vs_events_missed = true;
548 return NULL;
549 }
550
551 evt->event.event = event;
552 evt->event.reason = reason;
553 vs->vs_events_nr++;
554
555 return evt;
556}
557
Asias He3c63f662013-05-06 16:38:29 +0800558static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700559{
Asias He3c63f662013-05-06 16:38:29 +0800560 struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700561
562 /* TODO locking against target/backend threads? */
563 transport_generic_free_cmd(se_cmd, 1);
564
Asias He3c63f662013-05-06 16:38:29 +0800565 if (cmd->tvc_sgl_count) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700566 u32 i;
Asias He3c63f662013-05-06 16:38:29 +0800567 for (i = 0; i < cmd->tvc_sgl_count; i++)
568 put_page(sg_page(&cmd->tvc_sgl[i]));
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700569
Asias He3c63f662013-05-06 16:38:29 +0800570 kfree(cmd->tvc_sgl);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700571 }
572
Asias He3c63f662013-05-06 16:38:29 +0800573 tcm_vhost_put_inflight(cmd->inflight);
Asias Hef2f0173d2013-04-27 11:16:49 +0800574
Asias He3c63f662013-05-06 16:38:29 +0800575 kfree(cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700576}
577
Asias He683bd962013-05-06 16:38:27 +0800578static void
579tcm_vhost_do_evt_work(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
Asias Hea6c9af82013-04-25 15:35:21 +0800580{
Asias He3ab2e422013-04-27 11:16:48 +0800581 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Asias Hea6c9af82013-04-25 15:35:21 +0800582 struct virtio_scsi_event *event = &evt->event;
583 struct virtio_scsi_event __user *eventp;
584 unsigned out, in;
585 int head, ret;
586
587 if (!vq->private_data) {
588 vs->vs_events_missed = true;
589 return;
590 }
591
592again:
593 vhost_disable_notify(&vs->dev, vq);
594 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
595 ARRAY_SIZE(vq->iov), &out, &in,
596 NULL, NULL);
597 if (head < 0) {
598 vs->vs_events_missed = true;
599 return;
600 }
601 if (head == vq->num) {
602 if (vhost_enable_notify(&vs->dev, vq))
603 goto again;
604 vs->vs_events_missed = true;
605 return;
606 }
607
608 if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) {
609 vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n",
610 vq->iov[out].iov_len);
611 vs->vs_events_missed = true;
612 return;
613 }
614
615 if (vs->vs_events_missed) {
616 event->event |= VIRTIO_SCSI_T_EVENTS_MISSED;
617 vs->vs_events_missed = false;
618 }
619
620 eventp = vq->iov[out].iov_base;
621 ret = __copy_to_user(eventp, event, sizeof(*event));
622 if (!ret)
623 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
624 else
625 vq_err(vq, "Faulted on tcm_vhost_send_event\n");
626}
627
628static void tcm_vhost_evt_work(struct vhost_work *work)
629{
630 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
631 vs_event_work);
Asias He3ab2e422013-04-27 11:16:48 +0800632 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Asias Hea6c9af82013-04-25 15:35:21 +0800633 struct tcm_vhost_evt *evt;
634 struct llist_node *llnode;
635
636 mutex_lock(&vq->mutex);
637 llnode = llist_del_all(&vs->vs_event_list);
638 while (llnode) {
639 evt = llist_entry(llnode, struct tcm_vhost_evt, list);
640 llnode = llist_next(llnode);
641 tcm_vhost_do_evt_work(vs, evt);
642 tcm_vhost_free_evt(vs, evt);
643 }
644 mutex_unlock(&vq->mutex);
645}
646
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700647/* Fill in status and signal that we are done processing this command
648 *
649 * This is scheduled in the vhost work queue so we are called with the owner
650 * process mm and can access the vring.
651 */
652static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
653{
654 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
655 vs_completion_work);
Asias He1b7f3902013-02-06 13:20:59 +0800656 DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
Asias He9d6064a2013-01-06 14:36:13 +0800657 struct virtio_scsi_cmd_resp v_rsp;
Asias He3c63f662013-05-06 16:38:29 +0800658 struct tcm_vhost_cmd *cmd;
Asias He9d6064a2013-01-06 14:36:13 +0800659 struct llist_node *llnode;
660 struct se_cmd *se_cmd;
Asias He1b7f3902013-02-06 13:20:59 +0800661 int ret, vq;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700662
Asias He1b7f3902013-02-06 13:20:59 +0800663 bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
Asias He9d6064a2013-01-06 14:36:13 +0800664 llnode = llist_del_all(&vs->vs_completion_list);
665 while (llnode) {
Asias He3c63f662013-05-06 16:38:29 +0800666 cmd = llist_entry(llnode, struct tcm_vhost_cmd,
Asias He9d6064a2013-01-06 14:36:13 +0800667 tvc_completion_list);
668 llnode = llist_next(llnode);
Asias He3c63f662013-05-06 16:38:29 +0800669 se_cmd = &cmd->tvc_se_cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700670
671 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
Asias He3c63f662013-05-06 16:38:29 +0800672 cmd, se_cmd->residual_count, se_cmd->scsi_status);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700673
674 memset(&v_rsp, 0, sizeof(v_rsp));
675 v_rsp.resid = se_cmd->residual_count;
676 /* TODO is status_qualifier field needed? */
677 v_rsp.status = se_cmd->scsi_status;
678 v_rsp.sense_len = se_cmd->scsi_sense_length;
Asias He3c63f662013-05-06 16:38:29 +0800679 memcpy(v_rsp.sense, cmd->tvc_sense_buf,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700680 v_rsp.sense_len);
Asias He3c63f662013-05-06 16:38:29 +0800681 ret = copy_to_user(cmd->tvc_resp, &v_rsp, sizeof(v_rsp));
Asias He1b7f3902013-02-06 13:20:59 +0800682 if (likely(ret == 0)) {
Asias He3ab2e422013-04-27 11:16:48 +0800683 struct vhost_scsi_virtqueue *q;
Asias He3c63f662013-05-06 16:38:29 +0800684 vhost_add_used(cmd->tvc_vq, cmd->tvc_vq_desc, 0);
685 q = container_of(cmd->tvc_vq, struct vhost_scsi_virtqueue, vq);
Asias He3ab2e422013-04-27 11:16:48 +0800686 vq = q - vs->vqs;
Asias He1b7f3902013-02-06 13:20:59 +0800687 __set_bit(vq, signal);
688 } else
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700689 pr_err("Faulted on virtio_scsi_cmd_resp\n");
690
Asias He3c63f662013-05-06 16:38:29 +0800691 vhost_scsi_free_cmd(cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700692 }
693
Asias He1b7f3902013-02-06 13:20:59 +0800694 vq = -1;
695 while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1))
696 < VHOST_SCSI_MAX_VQ)
Asias He3ab2e422013-04-27 11:16:48 +0800697 vhost_signal(&vs->dev, &vs->vqs[vq].vq);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700698}
699
Asias He683bd962013-05-06 16:38:27 +0800700static struct tcm_vhost_cmd *
701vhost_scsi_allocate_cmd(struct vhost_virtqueue *vq,
Asias He98718312013-05-06 16:38:28 +0800702 struct tcm_vhost_tpg *tpg,
Asias He683bd962013-05-06 16:38:27 +0800703 struct virtio_scsi_cmd_req *v_req,
704 u32 exp_data_len,
705 int data_direction)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700706{
Asias He3c63f662013-05-06 16:38:29 +0800707 struct tcm_vhost_cmd *cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700708 struct tcm_vhost_nexus *tv_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700709
Asias He98718312013-05-06 16:38:28 +0800710 tv_nexus = tpg->tpg_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700711 if (!tv_nexus) {
712 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
713 return ERR_PTR(-EIO);
714 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700715
Asias He3c63f662013-05-06 16:38:29 +0800716 cmd = kzalloc(sizeof(struct tcm_vhost_cmd), GFP_ATOMIC);
717 if (!cmd) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700718 pr_err("Unable to allocate struct tcm_vhost_cmd\n");
719 return ERR_PTR(-ENOMEM);
720 }
Asias He3c63f662013-05-06 16:38:29 +0800721 cmd->tvc_tag = v_req->tag;
722 cmd->tvc_task_attr = v_req->task_attr;
723 cmd->tvc_exp_data_len = exp_data_len;
724 cmd->tvc_data_direction = data_direction;
725 cmd->tvc_nexus = tv_nexus;
726 cmd->inflight = tcm_vhost_get_inflight(vq);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700727
Asias He3c63f662013-05-06 16:38:29 +0800728 return cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700729}
730
731/*
732 * Map a user memory range into a scatterlist
733 *
734 * Returns the number of scatterlist entries used or -errno on error.
735 */
Asias He683bd962013-05-06 16:38:27 +0800736static int
737vhost_scsi_map_to_sgl(struct scatterlist *sgl,
738 unsigned int sgl_count,
739 struct iovec *iov,
740 int write)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700741{
Asias He18100532013-01-22 11:20:27 +0800742 unsigned int npages = 0, pages_nr, offset, nbytes;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700743 struct scatterlist *sg = sgl;
Asias He18100532013-01-22 11:20:27 +0800744 void __user *ptr = iov->iov_base;
745 size_t len = iov->iov_len;
746 struct page **pages;
747 int ret, i;
748
749 pages_nr = iov_num_pages(iov);
750 if (pages_nr > sgl_count)
751 return -ENOBUFS;
752
753 pages = kmalloc(pages_nr * sizeof(struct page *), GFP_KERNEL);
754 if (!pages)
755 return -ENOMEM;
756
757 ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages);
758 /* No pages were pinned */
759 if (ret < 0)
760 goto out;
761 /* Less pages pinned than wanted */
762 if (ret != pages_nr) {
763 for (i = 0; i < ret; i++)
764 put_page(pages[i]);
765 ret = -EFAULT;
766 goto out;
767 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700768
769 while (len > 0) {
Asias He18100532013-01-22 11:20:27 +0800770 offset = (uintptr_t)ptr & ~PAGE_MASK;
771 nbytes = min_t(unsigned int, PAGE_SIZE - offset, len);
772 sg_set_page(sg, pages[npages], nbytes, offset);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700773 ptr += nbytes;
774 len -= nbytes;
775 sg++;
776 npages++;
777 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700778
Asias He18100532013-01-22 11:20:27 +0800779out:
780 kfree(pages);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700781 return ret;
782}
783
Asias He683bd962013-05-06 16:38:27 +0800784static int
Asias He3c63f662013-05-06 16:38:29 +0800785vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *cmd,
Asias He683bd962013-05-06 16:38:27 +0800786 struct iovec *iov,
787 unsigned int niov,
788 int write)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700789{
790 int ret;
791 unsigned int i;
792 u32 sgl_count;
793 struct scatterlist *sg;
794
795 /*
796 * Find out how long sglist needs to be
797 */
798 sgl_count = 0;
Asias Hef3158f32013-01-22 11:20:26 +0800799 for (i = 0; i < niov; i++)
800 sgl_count += iov_num_pages(&iov[i]);
801
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700802 /* TODO overflow checking */
803
Asias He3c63f662013-05-06 16:38:29 +0800804 sg = kmalloc(sizeof(cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700805 if (!sg)
806 return -ENOMEM;
Fengguang Wuf0e0e9b2012-07-30 13:19:07 -0700807 pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__,
808 sg, sgl_count, !sg);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700809 sg_init_table(sg, sgl_count);
810
Asias He3c63f662013-05-06 16:38:29 +0800811 cmd->tvc_sgl = sg;
812 cmd->tvc_sgl_count = sgl_count;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700813
814 pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count);
815 for (i = 0; i < niov; i++) {
Asias He18100532013-01-22 11:20:27 +0800816 ret = vhost_scsi_map_to_sgl(sg, sgl_count, &iov[i], write);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700817 if (ret < 0) {
Asias He3c63f662013-05-06 16:38:29 +0800818 for (i = 0; i < cmd->tvc_sgl_count; i++)
819 put_page(sg_page(&cmd->tvc_sgl[i]));
820 kfree(cmd->tvc_sgl);
821 cmd->tvc_sgl = NULL;
822 cmd->tvc_sgl_count = 0;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700823 return ret;
824 }
825
826 sg += ret;
827 sgl_count -= ret;
828 }
829 return 0;
830}
831
832static void tcm_vhost_submission_work(struct work_struct *work)
833{
Asias He3c63f662013-05-06 16:38:29 +0800834 struct tcm_vhost_cmd *cmd =
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700835 container_of(work, struct tcm_vhost_cmd, work);
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700836 struct tcm_vhost_nexus *tv_nexus;
Asias He3c63f662013-05-06 16:38:29 +0800837 struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700838 struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL;
839 int rc, sg_no_bidi = 0;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700840
Asias He3c63f662013-05-06 16:38:29 +0800841 if (cmd->tvc_sgl_count) {
842 sg_ptr = cmd->tvc_sgl;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700843/* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
844#if 0
845 if (se_cmd->se_cmd_flags & SCF_BIDI) {
846 sg_bidi_ptr = NULL;
847 sg_no_bidi = 0;
848 }
849#endif
850 } else {
851 sg_ptr = NULL;
852 }
Asias He3c63f662013-05-06 16:38:29 +0800853 tv_nexus = cmd->tvc_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700854
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700855 rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
Asias He3c63f662013-05-06 16:38:29 +0800856 cmd->tvc_cdb, &cmd->tvc_sense_buf[0],
857 cmd->tvc_lun, cmd->tvc_exp_data_len,
858 cmd->tvc_task_attr, cmd->tvc_data_direction,
859 0, sg_ptr, cmd->tvc_sgl_count,
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700860 sg_bidi_ptr, sg_no_bidi);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700861 if (rc < 0) {
862 transport_send_check_condition_and_sense(se_cmd,
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700863 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700864 transport_generic_free_cmd(se_cmd, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700865 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700866}
867
Asias He683bd962013-05-06 16:38:27 +0800868static void
869vhost_scsi_send_bad_target(struct vhost_scsi *vs,
870 struct vhost_virtqueue *vq,
871 int head, unsigned out)
Asias He637ab212013-04-10 15:06:15 +0800872{
873 struct virtio_scsi_cmd_resp __user *resp;
874 struct virtio_scsi_cmd_resp rsp;
875 int ret;
876
877 memset(&rsp, 0, sizeof(rsp));
878 rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
879 resp = vq->iov[out].iov_base;
880 ret = __copy_to_user(resp, &rsp, sizeof(rsp));
881 if (!ret)
882 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
883 else
884 pr_err("Faulted on virtio_scsi_cmd_resp\n");
885}
886
Asias He683bd962013-05-06 16:38:27 +0800887static void
888vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700889{
Asias He4f7f46d2013-04-03 14:17:37 +0800890 struct tcm_vhost_tpg **vs_tpg;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700891 struct virtio_scsi_cmd_req v_req;
Asias He98718312013-05-06 16:38:28 +0800892 struct tcm_vhost_tpg *tpg;
Asias He3c63f662013-05-06 16:38:29 +0800893 struct tcm_vhost_cmd *cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700894 u32 exp_data_len, data_first, data_num, data_direction;
895 unsigned out, in, i;
896 int head, ret;
Asias He67e18cf2013-02-05 12:31:57 +0800897 u8 target;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700898
Asias He4f7f46d2013-04-03 14:17:37 +0800899 /*
900 * We can handle the vq only after the endpoint is setup by calling the
901 * VHOST_SCSI_SET_ENDPOINT ioctl.
902 *
903 * TODO: Check that we are running from vhost_worker which acts
904 * as read-side critical section for vhost kind of RCU.
905 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
906 */
907 vs_tpg = rcu_dereference_check(vq->private_data, 1);
908 if (!vs_tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700909 return;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700910
911 mutex_lock(&vq->mutex);
912 vhost_disable_notify(&vs->dev, vq);
913
914 for (;;) {
915 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
916 ARRAY_SIZE(vq->iov), &out, &in,
917 NULL, NULL);
918 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
919 head, out, in);
920 /* On error, stop handling until the next kick. */
921 if (unlikely(head < 0))
922 break;
923 /* Nothing new? Wait for eventfd to tell us they refilled. */
924 if (head == vq->num) {
925 if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
926 vhost_disable_notify(&vs->dev, vq);
927 continue;
928 }
929 break;
930 }
931
932/* FIXME: BIDI operation */
933 if (out == 1 && in == 1) {
934 data_direction = DMA_NONE;
935 data_first = 0;
936 data_num = 0;
937 } else if (out == 1 && in > 1) {
938 data_direction = DMA_FROM_DEVICE;
939 data_first = out + 1;
940 data_num = in - 1;
941 } else if (out > 1 && in == 1) {
942 data_direction = DMA_TO_DEVICE;
943 data_first = 1;
944 data_num = out - 1;
945 } else {
946 vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
947 out, in);
948 break;
949 }
950
951 /*
952 * Check for a sane resp buffer so we can report errors to
953 * the guest.
954 */
955 if (unlikely(vq->iov[out].iov_len !=
956 sizeof(struct virtio_scsi_cmd_resp))) {
957 vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
958 " bytes\n", vq->iov[out].iov_len);
959 break;
960 }
961
962 if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) {
963 vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu"
964 " bytes\n", vq->iov[0].iov_len);
965 break;
966 }
967 pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
968 " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req));
969 ret = __copy_from_user(&v_req, vq->iov[0].iov_base,
970 sizeof(v_req));
971 if (unlikely(ret)) {
972 vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
973 break;
974 }
975
Asias He67e18cf2013-02-05 12:31:57 +0800976 /* Extract the tpgt */
977 target = v_req.lun[1];
Asias He98718312013-05-06 16:38:28 +0800978 tpg = ACCESS_ONCE(vs_tpg[target]);
Asias He67e18cf2013-02-05 12:31:57 +0800979
980 /* Target does not exist, fail the request */
Asias He98718312013-05-06 16:38:28 +0800981 if (unlikely(!tpg)) {
Asias He637ab212013-04-10 15:06:15 +0800982 vhost_scsi_send_bad_target(vs, vq, head, out);
Asias He67e18cf2013-02-05 12:31:57 +0800983 continue;
984 }
985
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700986 exp_data_len = 0;
987 for (i = 0; i < data_num; i++)
988 exp_data_len += vq->iov[data_first + i].iov_len;
989
Asias He3c63f662013-05-06 16:38:29 +0800990 cmd = vhost_scsi_allocate_cmd(vq, tpg, &v_req,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700991 exp_data_len, data_direction);
Asias He3c63f662013-05-06 16:38:29 +0800992 if (IS_ERR(cmd)) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700993 vq_err(vq, "vhost_scsi_allocate_cmd failed %ld\n",
Asias He3c63f662013-05-06 16:38:29 +0800994 PTR_ERR(cmd));
Asias He055f6482013-04-10 15:06:16 +0800995 goto err_cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700996 }
997 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
Asias He3c63f662013-05-06 16:38:29 +0800998 ": %d\n", cmd, exp_data_len, data_direction);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700999
Asias He3c63f662013-05-06 16:38:29 +08001000 cmd->tvc_vhost = vs;
1001 cmd->tvc_vq = vq;
1002 cmd->tvc_resp = vq->iov[out].iov_base;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001003
1004 /*
Asias He3c63f662013-05-06 16:38:29 +08001005 * Copy in the recieved CDB descriptor into cmd->tvc_cdb
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001006 * that will be used by tcm_vhost_new_cmd_map() and down into
1007 * target_setup_cmd_from_cdb()
1008 */
Asias He3c63f662013-05-06 16:38:29 +08001009 memcpy(cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001010 /*
1011 * Check that the recieved CDB size does not exceeded our
1012 * hardcoded max for tcm_vhost
1013 */
1014 /* TODO what if cdb was too small for varlen cdb header? */
Asias He3c63f662013-05-06 16:38:29 +08001015 if (unlikely(scsi_command_size(cmd->tvc_cdb) >
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001016 TCM_VHOST_MAX_CDB_SIZE)) {
1017 vq_err(vq, "Received SCSI CDB with command_size: %d that"
1018 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
Asias He3c63f662013-05-06 16:38:29 +08001019 scsi_command_size(cmd->tvc_cdb),
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001020 TCM_VHOST_MAX_CDB_SIZE);
Asias He055f6482013-04-10 15:06:16 +08001021 goto err_free;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001022 }
Asias He3c63f662013-05-06 16:38:29 +08001023 cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001024
1025 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
Asias He3c63f662013-05-06 16:38:29 +08001026 cmd->tvc_cdb[0], cmd->tvc_lun);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001027
1028 if (data_direction != DMA_NONE) {
Asias He3c63f662013-05-06 16:38:29 +08001029 ret = vhost_scsi_map_iov_to_sgl(cmd,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001030 &vq->iov[data_first], data_num,
1031 data_direction == DMA_TO_DEVICE);
1032 if (unlikely(ret)) {
1033 vq_err(vq, "Failed to map iov to sgl\n");
Asias He055f6482013-04-10 15:06:16 +08001034 goto err_free;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001035 }
1036 }
1037
1038 /*
1039 * Save the descriptor from vhost_get_vq_desc() to be used to
1040 * complete the virtio-scsi request in TCM callback context via
1041 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
1042 */
Asias He3c63f662013-05-06 16:38:29 +08001043 cmd->tvc_vq_desc = head;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001044 /*
1045 * Dispatch tv_cmd descriptor for cmwq execution in process
1046 * context provided by tcm_vhost_workqueue. This also ensures
1047 * tv_cmd is executed on the same kworker CPU as this vhost
1048 * thread to gain positive L2 cache locality effects..
1049 */
Asias He3c63f662013-05-06 16:38:29 +08001050 INIT_WORK(&cmd->work, tcm_vhost_submission_work);
1051 queue_work(tcm_vhost_workqueue, &cmd->work);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001052 }
1053
1054 mutex_unlock(&vq->mutex);
Asias He7ea206c2013-04-10 15:06:14 +08001055 return;
1056
Asias He055f6482013-04-10 15:06:16 +08001057err_free:
Asias He3c63f662013-05-06 16:38:29 +08001058 vhost_scsi_free_cmd(cmd);
Asias He055f6482013-04-10 15:06:16 +08001059err_cmd:
1060 vhost_scsi_send_bad_target(vs, vq, head, out);
Asias He7ea206c2013-04-10 15:06:14 +08001061 mutex_unlock(&vq->mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001062}
1063
1064static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
1065{
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001066 pr_debug("%s: The handling func for control queue.\n", __func__);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001067}
1068
Asias He683bd962013-05-06 16:38:27 +08001069static void
1070tcm_vhost_send_evt(struct vhost_scsi *vs,
1071 struct tcm_vhost_tpg *tpg,
1072 struct se_lun *lun,
1073 u32 event,
1074 u32 reason)
Asias Hea6c9af82013-04-25 15:35:21 +08001075{
1076 struct tcm_vhost_evt *evt;
1077
1078 evt = tcm_vhost_allocate_evt(vs, event, reason);
1079 if (!evt)
1080 return;
1081
1082 if (tpg && lun) {
1083 /* TODO: share lun setup code with virtio-scsi.ko */
1084 /*
1085 * Note: evt->event is zeroed when we allocate it and
1086 * lun[4-7] need to be zero according to virtio-scsi spec.
1087 */
1088 evt->event.lun[0] = 0x01;
1089 evt->event.lun[1] = tpg->tport_tpgt & 0xFF;
1090 if (lun->unpacked_lun >= 256)
1091 evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
1092 evt->event.lun[3] = lun->unpacked_lun & 0xFF;
1093 }
1094
1095 llist_add(&evt->list, &vs->vs_event_list);
1096 vhost_work_queue(&vs->dev, &vs->vs_event_work);
1097}
1098
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001099static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
1100{
Asias Hea6c9af82013-04-25 15:35:21 +08001101 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1102 poll.work);
1103 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1104
1105 mutex_lock(&vq->mutex);
1106 if (!vq->private_data)
1107 goto out;
1108
1109 if (vs->vs_events_missed)
1110 tcm_vhost_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
1111out:
1112 mutex_unlock(&vq->mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001113}
1114
1115static void vhost_scsi_handle_kick(struct vhost_work *work)
1116{
1117 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1118 poll.work);
1119 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1120
Asias He1b7f3902013-02-06 13:20:59 +08001121 vhost_scsi_handle_vq(vs, vq);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001122}
1123
Asias He4f7f46d2013-04-03 14:17:37 +08001124static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
1125{
Asias He3ab2e422013-04-27 11:16:48 +08001126 vhost_poll_flush(&vs->vqs[index].vq.poll);
Asias He4f7f46d2013-04-03 14:17:37 +08001127}
1128
Michael S. Tsirkin3dfbff32013-04-28 15:38:52 +03001129/* Callers must hold dev mutex */
Asias He4f7f46d2013-04-03 14:17:37 +08001130static void vhost_scsi_flush(struct vhost_scsi *vs)
1131{
Asias Hef2f0173d2013-04-27 11:16:49 +08001132 struct vhost_scsi_inflight *old_inflight[VHOST_SCSI_MAX_VQ];
Asias He4f7f46d2013-04-03 14:17:37 +08001133 int i;
1134
Asias Hef2f0173d2013-04-27 11:16:49 +08001135 /* Init new inflight and remember the old inflight */
1136 tcm_vhost_init_inflight(vs, old_inflight);
1137
1138 /*
1139 * The inflight->kref was initialized to 1. We decrement it here to
1140 * indicate the start of the flush operation so that it will reach 0
1141 * when all the reqs are finished.
1142 */
1143 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1144 kref_put(&old_inflight[i]->kref, tcm_vhost_done_inflight);
1145
1146 /* Flush both the vhost poll and vhost work */
Asias He4f7f46d2013-04-03 14:17:37 +08001147 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1148 vhost_scsi_flush_vq(vs, i);
1149 vhost_work_flush(&vs->dev, &vs->vs_completion_work);
Asias Hea6c9af82013-04-25 15:35:21 +08001150 vhost_work_flush(&vs->dev, &vs->vs_event_work);
Asias Hef2f0173d2013-04-27 11:16:49 +08001151
1152 /* Wait for all reqs issued before the flush to be finished */
1153 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1154 wait_for_completion(&old_inflight[i]->comp);
Asias He4f7f46d2013-04-03 14:17:37 +08001155}
1156
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001157/*
1158 * Called from vhost_scsi_ioctl() context to walk the list of available
1159 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
Asias Hef2b7daf2013-04-25 15:35:20 +08001160 *
1161 * The lock nesting rule is:
1162 * tcm_vhost_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001163 */
Asias He683bd962013-05-06 16:38:27 +08001164static int
1165vhost_scsi_set_endpoint(struct vhost_scsi *vs,
1166 struct vhost_scsi_target *t)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001167{
1168 struct tcm_vhost_tport *tv_tport;
Asias He98718312013-05-06 16:38:28 +08001169 struct tcm_vhost_tpg *tpg;
Asias He4f7f46d2013-04-03 14:17:37 +08001170 struct tcm_vhost_tpg **vs_tpg;
1171 struct vhost_virtqueue *vq;
1172 int index, ret, i, len;
Asias He67e18cf2013-02-05 12:31:57 +08001173 bool match = false;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001174
Asias Hef2b7daf2013-04-25 15:35:20 +08001175 mutex_lock(&tcm_vhost_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001176 mutex_lock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001177
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001178 /* Verify that ring has been setup correctly. */
1179 for (index = 0; index < vs->dev.nvqs; ++index) {
1180 /* Verify that ring has been setup correctly. */
Asias He3ab2e422013-04-27 11:16:48 +08001181 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
Asias Hef2b7daf2013-04-25 15:35:20 +08001182 ret = -EFAULT;
1183 goto out;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001184 }
1185 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001186
Asias He4f7f46d2013-04-03 14:17:37 +08001187 len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
1188 vs_tpg = kzalloc(len, GFP_KERNEL);
1189 if (!vs_tpg) {
Asias Hef2b7daf2013-04-25 15:35:20 +08001190 ret = -ENOMEM;
1191 goto out;
Asias He4f7f46d2013-04-03 14:17:37 +08001192 }
1193 if (vs->vs_tpg)
1194 memcpy(vs_tpg, vs->vs_tpg, len);
1195
Asias He98718312013-05-06 16:38:28 +08001196 list_for_each_entry(tpg, &tcm_vhost_list, tv_tpg_list) {
1197 mutex_lock(&tpg->tv_tpg_mutex);
1198 if (!tpg->tpg_nexus) {
1199 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001200 continue;
1201 }
Asias He98718312013-05-06 16:38:28 +08001202 if (tpg->tv_tpg_vhost_count != 0) {
1203 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001204 continue;
1205 }
Asias He98718312013-05-06 16:38:28 +08001206 tv_tport = tpg->tport;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001207
Asias He67e18cf2013-02-05 12:31:57 +08001208 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
Asias He98718312013-05-06 16:38:28 +08001209 if (vs->vs_tpg && vs->vs_tpg[tpg->tport_tpgt]) {
Asias He4f7f46d2013-04-03 14:17:37 +08001210 kfree(vs_tpg);
Asias He98718312013-05-06 16:38:28 +08001211 mutex_unlock(&tpg->tv_tpg_mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001212 ret = -EEXIST;
1213 goto out;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001214 }
Asias He98718312013-05-06 16:38:28 +08001215 tpg->tv_tpg_vhost_count++;
1216 tpg->vhost_scsi = vs;
1217 vs_tpg[tpg->tport_tpgt] = tpg;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001218 smp_mb__after_atomic_inc();
Asias He67e18cf2013-02-05 12:31:57 +08001219 match = true;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001220 }
Asias He98718312013-05-06 16:38:28 +08001221 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001222 }
Asias He67e18cf2013-02-05 12:31:57 +08001223
1224 if (match) {
1225 memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
1226 sizeof(vs->vs_vhost_wwpn));
Asias He4f7f46d2013-04-03 14:17:37 +08001227 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
Asias He3ab2e422013-04-27 11:16:48 +08001228 vq = &vs->vqs[i].vq;
Asias He4f7f46d2013-04-03 14:17:37 +08001229 /* Flushing the vhost_work acts as synchronize_rcu */
1230 mutex_lock(&vq->mutex);
1231 rcu_assign_pointer(vq->private_data, vs_tpg);
Asias Hedfd5d562013-04-03 14:17:38 +08001232 vhost_init_used(vq);
Asias He4f7f46d2013-04-03 14:17:37 +08001233 mutex_unlock(&vq->mutex);
1234 }
Asias He67e18cf2013-02-05 12:31:57 +08001235 ret = 0;
1236 } else {
1237 ret = -EEXIST;
1238 }
1239
Asias He4f7f46d2013-04-03 14:17:37 +08001240 /*
1241 * Act as synchronize_rcu to make sure access to
1242 * old vs->vs_tpg is finished.
1243 */
1244 vhost_scsi_flush(vs);
1245 kfree(vs->vs_tpg);
1246 vs->vs_tpg = vs_tpg;
1247
Asias Hef2b7daf2013-04-25 15:35:20 +08001248out:
Asias He67e18cf2013-02-05 12:31:57 +08001249 mutex_unlock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001250 mutex_unlock(&tcm_vhost_mutex);
Asias He67e18cf2013-02-05 12:31:57 +08001251 return ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001252}
1253
Asias He683bd962013-05-06 16:38:27 +08001254static int
1255vhost_scsi_clear_endpoint(struct vhost_scsi *vs,
1256 struct vhost_scsi_target *t)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001257{
1258 struct tcm_vhost_tport *tv_tport;
Asias He98718312013-05-06 16:38:28 +08001259 struct tcm_vhost_tpg *tpg;
Asias He4f7f46d2013-04-03 14:17:37 +08001260 struct vhost_virtqueue *vq;
1261 bool match = false;
Asias He67e18cf2013-02-05 12:31:57 +08001262 int index, ret, i;
1263 u8 target;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001264
Asias Hef2b7daf2013-04-25 15:35:20 +08001265 mutex_lock(&tcm_vhost_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001266 mutex_lock(&vs->dev.mutex);
1267 /* Verify that ring has been setup correctly. */
1268 for (index = 0; index < vs->dev.nvqs; ++index) {
Asias He3ab2e422013-04-27 11:16:48 +08001269 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001270 ret = -EFAULT;
Asias He038e0af2013-03-15 09:14:05 +08001271 goto err_dev;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001272 }
1273 }
Asias He4f7f46d2013-04-03 14:17:37 +08001274
1275 if (!vs->vs_tpg) {
Asias Hef2b7daf2013-04-25 15:35:20 +08001276 ret = 0;
1277 goto err_dev;
Asias He4f7f46d2013-04-03 14:17:37 +08001278 }
1279
Asias He67e18cf2013-02-05 12:31:57 +08001280 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
1281 target = i;
Asias He98718312013-05-06 16:38:28 +08001282 tpg = vs->vs_tpg[target];
1283 if (!tpg)
Asias He67e18cf2013-02-05 12:31:57 +08001284 continue;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001285
Asias He98718312013-05-06 16:38:28 +08001286 mutex_lock(&tpg->tv_tpg_mutex);
1287 tv_tport = tpg->tport;
Asias He67e18cf2013-02-05 12:31:57 +08001288 if (!tv_tport) {
1289 ret = -ENODEV;
Asias He038e0af2013-03-15 09:14:05 +08001290 goto err_tpg;
Asias He67e18cf2013-02-05 12:31:57 +08001291 }
1292
1293 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
Asias He98718312013-05-06 16:38:28 +08001294 pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu"
Asias He67e18cf2013-02-05 12:31:57 +08001295 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
Asias He98718312013-05-06 16:38:28 +08001296 tv_tport->tport_name, tpg->tport_tpgt,
Asias He67e18cf2013-02-05 12:31:57 +08001297 t->vhost_wwpn, t->vhost_tpgt);
1298 ret = -EINVAL;
Asias He038e0af2013-03-15 09:14:05 +08001299 goto err_tpg;
Asias He67e18cf2013-02-05 12:31:57 +08001300 }
Asias He98718312013-05-06 16:38:28 +08001301 tpg->tv_tpg_vhost_count--;
1302 tpg->vhost_scsi = NULL;
Asias He67e18cf2013-02-05 12:31:57 +08001303 vs->vs_tpg[target] = NULL;
Asias He4f7f46d2013-04-03 14:17:37 +08001304 match = true;
Asias He98718312013-05-06 16:38:28 +08001305 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001306 }
Asias He4f7f46d2013-04-03 14:17:37 +08001307 if (match) {
1308 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 /* Flushing the vhost_work acts as synchronize_rcu */
1311 mutex_lock(&vq->mutex);
1312 rcu_assign_pointer(vq->private_data, NULL);
1313 mutex_unlock(&vq->mutex);
1314 }
1315 }
1316 /*
1317 * Act as synchronize_rcu to make sure access to
1318 * old vs->vs_tpg is finished.
1319 */
1320 vhost_scsi_flush(vs);
1321 kfree(vs->vs_tpg);
1322 vs->vs_tpg = NULL;
Asias Hea6c9af82013-04-25 15:35:21 +08001323 WARN_ON(vs->vs_events_nr);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001324 mutex_unlock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001325 mutex_unlock(&tcm_vhost_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001326 return 0;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001327
Asias He038e0af2013-03-15 09:14:05 +08001328err_tpg:
Asias He98718312013-05-06 16:38:28 +08001329 mutex_unlock(&tpg->tv_tpg_mutex);
Asias He038e0af2013-03-15 09:14:05 +08001330err_dev:
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001331 mutex_unlock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001332 mutex_unlock(&tcm_vhost_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001333 return ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001334}
1335
Asias He4f7f46d2013-04-03 14:17:37 +08001336static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
1337{
1338 if (features & ~VHOST_SCSI_FEATURES)
1339 return -EOPNOTSUPP;
1340
1341 mutex_lock(&vs->dev.mutex);
1342 if ((features & (1 << VHOST_F_LOG_ALL)) &&
1343 !vhost_log_access_ok(&vs->dev)) {
1344 mutex_unlock(&vs->dev.mutex);
1345 return -EFAULT;
1346 }
1347 vs->dev.acked_features = features;
1348 smp_wmb();
1349 vhost_scsi_flush(vs);
1350 mutex_unlock(&vs->dev.mutex);
1351 return 0;
1352}
1353
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001354static int vhost_scsi_open(struct inode *inode, struct file *f)
1355{
Asias Hec7289312013-05-06 16:38:26 +08001356 struct vhost_scsi *vs;
Asias He3ab2e422013-04-27 11:16:48 +08001357 struct vhost_virtqueue **vqs;
Asias He1b7f3902013-02-06 13:20:59 +08001358 int r, i;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001359
Asias Hec7289312013-05-06 16:38:26 +08001360 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
1361 if (!vs)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001362 return -ENOMEM;
1363
Asias He3ab2e422013-04-27 11:16:48 +08001364 vqs = kmalloc(VHOST_SCSI_MAX_VQ * sizeof(*vqs), GFP_KERNEL);
1365 if (!vqs) {
Asias Hec7289312013-05-06 16:38:26 +08001366 kfree(vs);
Asias He3ab2e422013-04-27 11:16:48 +08001367 return -ENOMEM;
1368 }
1369
Asias Hec7289312013-05-06 16:38:26 +08001370 vhost_work_init(&vs->vs_completion_work, vhost_scsi_complete_cmd_work);
1371 vhost_work_init(&vs->vs_event_work, tcm_vhost_evt_work);
Asias Hea6c9af82013-04-25 15:35:21 +08001372
Asias Hec7289312013-05-06 16:38:26 +08001373 vs->vs_events_nr = 0;
1374 vs->vs_events_missed = false;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001375
Asias Hec7289312013-05-06 16:38:26 +08001376 vqs[VHOST_SCSI_VQ_CTL] = &vs->vqs[VHOST_SCSI_VQ_CTL].vq;
1377 vqs[VHOST_SCSI_VQ_EVT] = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1378 vs->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick;
1379 vs->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick;
Asias He3ab2e422013-04-27 11:16:48 +08001380 for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) {
Asias Hec7289312013-05-06 16:38:26 +08001381 vqs[i] = &vs->vqs[i].vq;
1382 vs->vqs[i].vq.handle_kick = vhost_scsi_handle_kick;
Asias He3ab2e422013-04-27 11:16:48 +08001383 }
Asias Hec7289312013-05-06 16:38:26 +08001384 r = vhost_dev_init(&vs->dev, vqs, VHOST_SCSI_MAX_VQ);
Asias Hef2f0173d2013-04-27 11:16:49 +08001385
Asias Hec7289312013-05-06 16:38:26 +08001386 tcm_vhost_init_inflight(vs, NULL);
Asias Hef2f0173d2013-04-27 11:16:49 +08001387
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001388 if (r < 0) {
Asias He3ab2e422013-04-27 11:16:48 +08001389 kfree(vqs);
Asias Hec7289312013-05-06 16:38:26 +08001390 kfree(vs);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001391 return r;
1392 }
1393
Asias Hec7289312013-05-06 16:38:26 +08001394 f->private_data = vs;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001395 return 0;
1396}
1397
1398static int vhost_scsi_release(struct inode *inode, struct file *f)
1399{
Asias Hec7289312013-05-06 16:38:26 +08001400 struct vhost_scsi *vs = f->private_data;
Asias He67e18cf2013-02-05 12:31:57 +08001401 struct vhost_scsi_target t;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001402
Asias Hec7289312013-05-06 16:38:26 +08001403 mutex_lock(&vs->dev.mutex);
1404 memcpy(t.vhost_wwpn, vs->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
1405 mutex_unlock(&vs->dev.mutex);
1406 vhost_scsi_clear_endpoint(vs, &t);
1407 vhost_dev_stop(&vs->dev);
1408 vhost_dev_cleanup(&vs->dev, false);
Asias Hea6c9af82013-04-25 15:35:21 +08001409 /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
Asias Hec7289312013-05-06 16:38:26 +08001410 vhost_scsi_flush(vs);
1411 kfree(vs->dev.vqs);
1412 kfree(vs);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001413 return 0;
1414}
1415
Asias He683bd962013-05-06 16:38:27 +08001416static long
1417vhost_scsi_ioctl(struct file *f,
1418 unsigned int ioctl,
1419 unsigned long arg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001420{
1421 struct vhost_scsi *vs = f->private_data;
1422 struct vhost_scsi_target backend;
1423 void __user *argp = (void __user *)arg;
1424 u64 __user *featurep = argp;
Asias He11c634182013-04-25 15:35:22 +08001425 u32 __user *eventsp = argp;
1426 u32 events_missed;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001427 u64 features;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001428 int r, abi_version = VHOST_SCSI_ABI_VERSION;
Asias He3ab2e422013-04-27 11:16:48 +08001429 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001430
1431 switch (ioctl) {
1432 case VHOST_SCSI_SET_ENDPOINT:
1433 if (copy_from_user(&backend, argp, sizeof backend))
1434 return -EFAULT;
Michael S. Tsirkin6de71452012-08-18 15:44:09 -07001435 if (backend.reserved != 0)
1436 return -EOPNOTSUPP;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001437
1438 return vhost_scsi_set_endpoint(vs, &backend);
1439 case VHOST_SCSI_CLEAR_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_clear_endpoint(vs, &backend);
1446 case VHOST_SCSI_GET_ABI_VERSION:
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001447 if (copy_to_user(argp, &abi_version, sizeof abi_version))
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001448 return -EFAULT;
1449 return 0;
Asias He11c634182013-04-25 15:35:22 +08001450 case VHOST_SCSI_SET_EVENTS_MISSED:
1451 if (get_user(events_missed, eventsp))
1452 return -EFAULT;
1453 mutex_lock(&vq->mutex);
1454 vs->vs_events_missed = events_missed;
1455 mutex_unlock(&vq->mutex);
1456 return 0;
1457 case VHOST_SCSI_GET_EVENTS_MISSED:
1458 mutex_lock(&vq->mutex);
1459 events_missed = vs->vs_events_missed;
1460 mutex_unlock(&vq->mutex);
1461 if (put_user(events_missed, eventsp))
1462 return -EFAULT;
1463 return 0;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001464 case VHOST_GET_FEATURES:
Nicholas Bellinger5dade712013-03-27 17:23:41 -07001465 features = VHOST_SCSI_FEATURES;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001466 if (copy_to_user(featurep, &features, sizeof features))
1467 return -EFAULT;
1468 return 0;
1469 case VHOST_SET_FEATURES:
1470 if (copy_from_user(&features, featurep, sizeof features))
1471 return -EFAULT;
1472 return vhost_scsi_set_features(vs, features);
1473 default:
1474 mutex_lock(&vs->dev.mutex);
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001475 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
1476 /* TODO: flush backend after dev ioctl. */
1477 if (r == -ENOIOCTLCMD)
1478 r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001479 mutex_unlock(&vs->dev.mutex);
1480 return r;
1481 }
1482}
1483
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001484#ifdef CONFIG_COMPAT
1485static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
1486 unsigned long arg)
1487{
1488 return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1489}
1490#endif
1491
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001492static const struct file_operations vhost_scsi_fops = {
1493 .owner = THIS_MODULE,
1494 .release = vhost_scsi_release,
1495 .unlocked_ioctl = vhost_scsi_ioctl,
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001496#ifdef CONFIG_COMPAT
1497 .compat_ioctl = vhost_scsi_compat_ioctl,
1498#endif
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001499 .open = vhost_scsi_open,
1500 .llseek = noop_llseek,
1501};
1502
1503static struct miscdevice vhost_scsi_misc = {
1504 MISC_DYNAMIC_MINOR,
1505 "vhost-scsi",
1506 &vhost_scsi_fops,
1507};
1508
1509static int __init vhost_scsi_register(void)
1510{
1511 return misc_register(&vhost_scsi_misc);
1512}
1513
1514static int vhost_scsi_deregister(void)
1515{
1516 return misc_deregister(&vhost_scsi_misc);
1517}
1518
1519static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
1520{
1521 switch (tport->tport_proto_id) {
1522 case SCSI_PROTOCOL_SAS:
1523 return "SAS";
1524 case SCSI_PROTOCOL_FCP:
1525 return "FCP";
1526 case SCSI_PROTOCOL_ISCSI:
1527 return "iSCSI";
1528 default:
1529 break;
1530 }
1531
1532 return "Unknown";
1533}
1534
Asias He683bd962013-05-06 16:38:27 +08001535static void
1536tcm_vhost_do_plug(struct tcm_vhost_tpg *tpg,
1537 struct se_lun *lun, bool plug)
Asias Hea6c9af82013-04-25 15:35:21 +08001538{
1539
1540 struct vhost_scsi *vs = tpg->vhost_scsi;
1541 struct vhost_virtqueue *vq;
1542 u32 reason;
1543
1544 if (!vs)
1545 return;
1546
1547 mutex_lock(&vs->dev.mutex);
1548 if (!vhost_has_feature(&vs->dev, VIRTIO_SCSI_F_HOTPLUG)) {
1549 mutex_unlock(&vs->dev.mutex);
1550 return;
1551 }
1552
1553 if (plug)
1554 reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
1555 else
1556 reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
1557
Asias He3ab2e422013-04-27 11:16:48 +08001558 vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Asias Hea6c9af82013-04-25 15:35:21 +08001559 mutex_lock(&vq->mutex);
1560 tcm_vhost_send_evt(vs, tpg, lun,
1561 VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
1562 mutex_unlock(&vq->mutex);
1563 mutex_unlock(&vs->dev.mutex);
1564}
1565
1566static void tcm_vhost_hotplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1567{
1568 tcm_vhost_do_plug(tpg, lun, true);
1569}
1570
1571static void tcm_vhost_hotunplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1572{
1573 tcm_vhost_do_plug(tpg, lun, false);
1574}
1575
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001576static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08001577 struct se_lun *lun)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001578{
Asias He98718312013-05-06 16:38:28 +08001579 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001580 struct tcm_vhost_tpg, se_tpg);
1581
Asias Hea6c9af82013-04-25 15:35:21 +08001582 mutex_lock(&tcm_vhost_mutex);
1583
Asias He98718312013-05-06 16:38:28 +08001584 mutex_lock(&tpg->tv_tpg_mutex);
1585 tpg->tv_tpg_port_count++;
1586 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001587
Asias He98718312013-05-06 16:38:28 +08001588 tcm_vhost_hotplug(tpg, lun);
Asias Hea6c9af82013-04-25 15:35:21 +08001589
1590 mutex_unlock(&tcm_vhost_mutex);
1591
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001592 return 0;
1593}
1594
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001595static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08001596 struct se_lun *lun)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001597{
Asias He98718312013-05-06 16:38:28 +08001598 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001599 struct tcm_vhost_tpg, se_tpg);
1600
Asias Hea6c9af82013-04-25 15:35:21 +08001601 mutex_lock(&tcm_vhost_mutex);
1602
Asias He98718312013-05-06 16:38:28 +08001603 mutex_lock(&tpg->tv_tpg_mutex);
1604 tpg->tv_tpg_port_count--;
1605 mutex_unlock(&tpg->tv_tpg_mutex);
Asias Hea6c9af82013-04-25 15:35:21 +08001606
Asias He98718312013-05-06 16:38:28 +08001607 tcm_vhost_hotunplug(tpg, lun);
Asias Hea6c9af82013-04-25 15:35:21 +08001608
1609 mutex_unlock(&tcm_vhost_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001610}
1611
Asias He683bd962013-05-06 16:38:27 +08001612static struct se_node_acl *
1613tcm_vhost_make_nodeacl(struct se_portal_group *se_tpg,
1614 struct config_group *group,
1615 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001616{
1617 struct se_node_acl *se_nacl, *se_nacl_new;
1618 struct tcm_vhost_nacl *nacl;
1619 u64 wwpn = 0;
1620 u32 nexus_depth;
1621
1622 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1623 return ERR_PTR(-EINVAL); */
1624 se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
1625 if (!se_nacl_new)
1626 return ERR_PTR(-ENOMEM);
1627
1628 nexus_depth = 1;
1629 /*
1630 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1631 * when converting a NodeACL from demo mode -> explict
1632 */
1633 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1634 name, nexus_depth);
1635 if (IS_ERR(se_nacl)) {
1636 tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
1637 return se_nacl;
1638 }
1639 /*
1640 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1641 */
1642 nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
1643 nacl->iport_wwpn = wwpn;
1644
1645 return se_nacl;
1646}
1647
1648static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
1649{
1650 struct tcm_vhost_nacl *nacl = container_of(se_acl,
1651 struct tcm_vhost_nacl, se_node_acl);
1652 core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1653 kfree(nacl);
1654}
1655
Asias He98718312013-05-06 16:38:28 +08001656static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tpg,
Asias He683bd962013-05-06 16:38:27 +08001657 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001658{
1659 struct se_portal_group *se_tpg;
1660 struct tcm_vhost_nexus *tv_nexus;
1661
Asias He98718312013-05-06 16:38:28 +08001662 mutex_lock(&tpg->tv_tpg_mutex);
1663 if (tpg->tpg_nexus) {
1664 mutex_unlock(&tpg->tv_tpg_mutex);
1665 pr_debug("tpg->tpg_nexus already exists\n");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001666 return -EEXIST;
1667 }
Asias He98718312013-05-06 16:38:28 +08001668 se_tpg = &tpg->se_tpg;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001669
1670 tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
1671 if (!tv_nexus) {
Asias He98718312013-05-06 16:38:28 +08001672 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001673 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1674 return -ENOMEM;
1675 }
1676 /*
1677 * Initialize the struct se_session pointer
1678 */
1679 tv_nexus->tvn_se_sess = transport_init_session();
1680 if (IS_ERR(tv_nexus->tvn_se_sess)) {
Asias He98718312013-05-06 16:38:28 +08001681 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001682 kfree(tv_nexus);
1683 return -ENOMEM;
1684 }
1685 /*
1686 * Since we are running in 'demo mode' this call with generate a
1687 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1688 * the SCSI Initiator port name of the passed configfs group 'name'.
1689 */
1690 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1691 se_tpg, (unsigned char *)name);
1692 if (!tv_nexus->tvn_se_sess->se_node_acl) {
Asias He98718312013-05-06 16:38:28 +08001693 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001694 pr_debug("core_tpg_check_initiator_node_acl() failed"
1695 " for %s\n", name);
1696 transport_free_session(tv_nexus->tvn_se_sess);
1697 kfree(tv_nexus);
1698 return -ENOMEM;
1699 }
1700 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001701 * Now register the TCM vhost virtual I_T Nexus as active with the
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001702 * call to __transport_register_session()
1703 */
1704 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1705 tv_nexus->tvn_se_sess, tv_nexus);
Asias He98718312013-05-06 16:38:28 +08001706 tpg->tpg_nexus = tv_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001707
Asias He98718312013-05-06 16:38:28 +08001708 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001709 return 0;
1710}
1711
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001712static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001713{
1714 struct se_session *se_sess;
1715 struct tcm_vhost_nexus *tv_nexus;
1716
1717 mutex_lock(&tpg->tv_tpg_mutex);
1718 tv_nexus = tpg->tpg_nexus;
1719 if (!tv_nexus) {
1720 mutex_unlock(&tpg->tv_tpg_mutex);
1721 return -ENODEV;
1722 }
1723
1724 se_sess = tv_nexus->tvn_se_sess;
1725 if (!se_sess) {
1726 mutex_unlock(&tpg->tv_tpg_mutex);
1727 return -ENODEV;
1728 }
1729
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001730 if (tpg->tv_tpg_port_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001731 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001732 pr_err("Unable to remove TCM_vhost I_T Nexus with"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001733 " active TPG port count: %d\n",
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001734 tpg->tv_tpg_port_count);
1735 return -EBUSY;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001736 }
1737
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001738 if (tpg->tv_tpg_vhost_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001739 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001740 pr_err("Unable to remove TCM_vhost I_T Nexus with"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001741 " active TPG vhost count: %d\n",
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001742 tpg->tv_tpg_vhost_count);
1743 return -EBUSY;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001744 }
1745
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001746 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001747 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
1748 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1749 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001750 * Release the SCSI I_T Nexus to the emulated vhost Target Port
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001751 */
1752 transport_deregister_session(tv_nexus->tvn_se_sess);
1753 tpg->tpg_nexus = NULL;
1754 mutex_unlock(&tpg->tv_tpg_mutex);
1755
1756 kfree(tv_nexus);
1757 return 0;
1758}
1759
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001760static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08001761 char *page)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001762{
Asias He98718312013-05-06 16:38:28 +08001763 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001764 struct tcm_vhost_tpg, se_tpg);
1765 struct tcm_vhost_nexus *tv_nexus;
1766 ssize_t ret;
1767
Asias He98718312013-05-06 16:38:28 +08001768 mutex_lock(&tpg->tv_tpg_mutex);
1769 tv_nexus = tpg->tpg_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001770 if (!tv_nexus) {
Asias He98718312013-05-06 16:38:28 +08001771 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001772 return -ENODEV;
1773 }
1774 ret = snprintf(page, PAGE_SIZE, "%s\n",
1775 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
Asias He98718312013-05-06 16:38:28 +08001776 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001777
1778 return ret;
1779}
1780
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001781static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08001782 const char *page,
1783 size_t count)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001784{
Asias He98718312013-05-06 16:38:28 +08001785 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001786 struct tcm_vhost_tpg, se_tpg);
Asias He98718312013-05-06 16:38:28 +08001787 struct tcm_vhost_tport *tport_wwn = tpg->tport;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001788 unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
1789 int ret;
1790 /*
1791 * Shutdown the active I_T nexus if 'NULL' is passed..
1792 */
1793 if (!strncmp(page, "NULL", 4)) {
Asias He98718312013-05-06 16:38:28 +08001794 ret = tcm_vhost_drop_nexus(tpg);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001795 return (!ret) ? count : ret;
1796 }
1797 /*
1798 * Otherwise make sure the passed virtual Initiator port WWN matches
1799 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
1800 * tcm_vhost_make_nexus().
1801 */
1802 if (strlen(page) >= TCM_VHOST_NAMELEN) {
1803 pr_err("Emulated NAA Sas Address: %s, exceeds"
1804 " max: %d\n", page, TCM_VHOST_NAMELEN);
1805 return -EINVAL;
1806 }
1807 snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
1808
1809 ptr = strstr(i_port, "naa.");
1810 if (ptr) {
1811 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1812 pr_err("Passed SAS Initiator Port %s does not"
1813 " match target port protoid: %s\n", i_port,
1814 tcm_vhost_dump_proto_id(tport_wwn));
1815 return -EINVAL;
1816 }
1817 port_ptr = &i_port[0];
1818 goto check_newline;
1819 }
1820 ptr = strstr(i_port, "fc.");
1821 if (ptr) {
1822 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1823 pr_err("Passed FCP Initiator Port %s does not"
1824 " match target port protoid: %s\n", i_port,
1825 tcm_vhost_dump_proto_id(tport_wwn));
1826 return -EINVAL;
1827 }
1828 port_ptr = &i_port[3]; /* Skip over "fc." */
1829 goto check_newline;
1830 }
1831 ptr = strstr(i_port, "iqn.");
1832 if (ptr) {
1833 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1834 pr_err("Passed iSCSI Initiator Port %s does not"
1835 " match target port protoid: %s\n", i_port,
1836 tcm_vhost_dump_proto_id(tport_wwn));
1837 return -EINVAL;
1838 }
1839 port_ptr = &i_port[0];
1840 goto check_newline;
1841 }
1842 pr_err("Unable to locate prefix for emulated Initiator Port:"
1843 " %s\n", i_port);
1844 return -EINVAL;
1845 /*
1846 * Clear any trailing newline for the NAA WWN
1847 */
1848check_newline:
1849 if (i_port[strlen(i_port)-1] == '\n')
1850 i_port[strlen(i_port)-1] = '\0';
1851
Asias He98718312013-05-06 16:38:28 +08001852 ret = tcm_vhost_make_nexus(tpg, port_ptr);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001853 if (ret < 0)
1854 return ret;
1855
1856 return count;
1857}
1858
1859TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
1860
1861static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
1862 &tcm_vhost_tpg_nexus.attr,
1863 NULL,
1864};
1865
Asias He683bd962013-05-06 16:38:27 +08001866static struct se_portal_group *
1867tcm_vhost_make_tpg(struct se_wwn *wwn,
1868 struct config_group *group,
1869 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001870{
1871 struct tcm_vhost_tport *tport = container_of(wwn,
1872 struct tcm_vhost_tport, tport_wwn);
1873
1874 struct tcm_vhost_tpg *tpg;
1875 unsigned long tpgt;
1876 int ret;
1877
1878 if (strstr(name, "tpgt_") != name)
1879 return ERR_PTR(-EINVAL);
1880 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
1881 return ERR_PTR(-EINVAL);
1882
1883 tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
1884 if (!tpg) {
1885 pr_err("Unable to allocate struct tcm_vhost_tpg");
1886 return ERR_PTR(-ENOMEM);
1887 }
1888 mutex_init(&tpg->tv_tpg_mutex);
1889 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1890 tpg->tport = tport;
1891 tpg->tport_tpgt = tpgt;
1892
1893 ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
1894 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1895 if (ret < 0) {
1896 kfree(tpg);
1897 return NULL;
1898 }
1899 mutex_lock(&tcm_vhost_mutex);
1900 list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
1901 mutex_unlock(&tcm_vhost_mutex);
1902
1903 return &tpg->se_tpg;
1904}
1905
1906static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
1907{
1908 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1909 struct tcm_vhost_tpg, se_tpg);
1910
1911 mutex_lock(&tcm_vhost_mutex);
1912 list_del(&tpg->tv_tpg_list);
1913 mutex_unlock(&tcm_vhost_mutex);
1914 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001915 * Release the virtual I_T Nexus for this vhost TPG
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001916 */
1917 tcm_vhost_drop_nexus(tpg);
1918 /*
1919 * Deregister the se_tpg from TCM..
1920 */
1921 core_tpg_deregister(se_tpg);
1922 kfree(tpg);
1923}
1924
Asias He683bd962013-05-06 16:38:27 +08001925static struct se_wwn *
1926tcm_vhost_make_tport(struct target_fabric_configfs *tf,
1927 struct config_group *group,
1928 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001929{
1930 struct tcm_vhost_tport *tport;
1931 char *ptr;
1932 u64 wwpn = 0;
1933 int off = 0;
1934
1935 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1936 return ERR_PTR(-EINVAL); */
1937
1938 tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
1939 if (!tport) {
1940 pr_err("Unable to allocate struct tcm_vhost_tport");
1941 return ERR_PTR(-ENOMEM);
1942 }
1943 tport->tport_wwpn = wwpn;
1944 /*
1945 * Determine the emulated Protocol Identifier and Target Port Name
1946 * based on the incoming configfs directory name.
1947 */
1948 ptr = strstr(name, "naa.");
1949 if (ptr) {
1950 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1951 goto check_len;
1952 }
1953 ptr = strstr(name, "fc.");
1954 if (ptr) {
1955 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1956 off = 3; /* Skip over "fc." */
1957 goto check_len;
1958 }
1959 ptr = strstr(name, "iqn.");
1960 if (ptr) {
1961 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1962 goto check_len;
1963 }
1964
1965 pr_err("Unable to locate prefix for emulated Target Port:"
1966 " %s\n", name);
1967 kfree(tport);
1968 return ERR_PTR(-EINVAL);
1969
1970check_len:
1971 if (strlen(name) >= TCM_VHOST_NAMELEN) {
1972 pr_err("Emulated %s Address: %s, exceeds"
1973 " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
1974 TCM_VHOST_NAMELEN);
1975 kfree(tport);
1976 return ERR_PTR(-EINVAL);
1977 }
1978 snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
1979
1980 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
1981 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
1982
1983 return &tport->tport_wwn;
1984}
1985
1986static void tcm_vhost_drop_tport(struct se_wwn *wwn)
1987{
1988 struct tcm_vhost_tport *tport = container_of(wwn,
1989 struct tcm_vhost_tport, tport_wwn);
1990
1991 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
1992 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
1993 tport->tport_name);
1994
1995 kfree(tport);
1996}
1997
Asias He683bd962013-05-06 16:38:27 +08001998static ssize_t
1999tcm_vhost_wwn_show_attr_version(struct target_fabric_configfs *tf,
2000 char *page)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002001{
2002 return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
2003 "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
2004 utsname()->machine);
2005}
2006
2007TF_WWN_ATTR_RO(tcm_vhost, version);
2008
2009static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
2010 &tcm_vhost_wwn_version.attr,
2011 NULL,
2012};
2013
2014static struct target_core_fabric_ops tcm_vhost_ops = {
2015 .get_fabric_name = tcm_vhost_get_fabric_name,
2016 .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
2017 .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
2018 .tpg_get_tag = tcm_vhost_get_tag,
2019 .tpg_get_default_depth = tcm_vhost_get_default_depth,
2020 .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
2021 .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
2022 .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
2023 .tpg_check_demo_mode = tcm_vhost_check_true,
2024 .tpg_check_demo_mode_cache = tcm_vhost_check_true,
2025 .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
2026 .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
2027 .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
2028 .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
2029 .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
2030 .release_cmd = tcm_vhost_release_cmd,
2031 .shutdown_session = tcm_vhost_shutdown_session,
2032 .close_session = tcm_vhost_close_session,
2033 .sess_get_index = tcm_vhost_sess_get_index,
2034 .sess_get_initiator_sid = NULL,
2035 .write_pending = tcm_vhost_write_pending,
2036 .write_pending_status = tcm_vhost_write_pending_status,
2037 .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
2038 .get_task_tag = tcm_vhost_get_task_tag,
2039 .get_cmd_state = tcm_vhost_get_cmd_state,
2040 .queue_data_in = tcm_vhost_queue_data_in,
2041 .queue_status = tcm_vhost_queue_status,
2042 .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002043 /*
2044 * Setup callers for generic logic in target_core_fabric_configfs.c
2045 */
2046 .fabric_make_wwn = tcm_vhost_make_tport,
2047 .fabric_drop_wwn = tcm_vhost_drop_tport,
2048 .fabric_make_tpg = tcm_vhost_make_tpg,
2049 .fabric_drop_tpg = tcm_vhost_drop_tpg,
2050 .fabric_post_link = tcm_vhost_port_link,
2051 .fabric_pre_unlink = tcm_vhost_port_unlink,
2052 .fabric_make_np = NULL,
2053 .fabric_drop_np = NULL,
2054 .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
2055 .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
2056};
2057
2058static int tcm_vhost_register_configfs(void)
2059{
2060 struct target_fabric_configfs *fabric;
2061 int ret;
2062
2063 pr_debug("TCM_VHOST fabric module %s on %s/%s"
2064 " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
2065 utsname()->machine);
2066 /*
2067 * Register the top level struct config_item_type with TCM core
2068 */
2069 fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
2070 if (IS_ERR(fabric)) {
2071 pr_err("target_fabric_configfs_init() failed\n");
2072 return PTR_ERR(fabric);
2073 }
2074 /*
2075 * Setup fabric->tf_ops from our local tcm_vhost_ops
2076 */
2077 fabric->tf_ops = tcm_vhost_ops;
2078 /*
2079 * Setup default attribute lists for various fabric->tf_cit_tmpl
2080 */
2081 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
2082 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
2083 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
2084 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
2085 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
2086 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
2087 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
2088 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
2089 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
2090 /*
2091 * Register the fabric for use within TCM
2092 */
2093 ret = target_fabric_configfs_register(fabric);
2094 if (ret < 0) {
2095 pr_err("target_fabric_configfs_register() failed"
2096 " for TCM_VHOST\n");
2097 return ret;
2098 }
2099 /*
2100 * Setup our local pointer to *fabric
2101 */
2102 tcm_vhost_fabric_configfs = fabric;
2103 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
2104 return 0;
2105};
2106
2107static void tcm_vhost_deregister_configfs(void)
2108{
2109 if (!tcm_vhost_fabric_configfs)
2110 return;
2111
2112 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
2113 tcm_vhost_fabric_configfs = NULL;
2114 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
2115};
2116
2117static int __init tcm_vhost_init(void)
2118{
2119 int ret = -ENOMEM;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002120 /*
2121 * Use our own dedicated workqueue for submitting I/O into
2122 * target core to avoid contention within system_wq.
2123 */
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002124 tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
2125 if (!tcm_vhost_workqueue)
2126 goto out;
2127
2128 ret = vhost_scsi_register();
2129 if (ret < 0)
2130 goto out_destroy_workqueue;
2131
2132 ret = tcm_vhost_register_configfs();
2133 if (ret < 0)
2134 goto out_vhost_scsi_deregister;
2135
2136 return 0;
2137
2138out_vhost_scsi_deregister:
2139 vhost_scsi_deregister();
2140out_destroy_workqueue:
2141 destroy_workqueue(tcm_vhost_workqueue);
2142out:
2143 return ret;
2144};
2145
2146static void tcm_vhost_exit(void)
2147{
2148 tcm_vhost_deregister_configfs();
2149 vhost_scsi_deregister();
2150 destroy_workqueue(tcm_vhost_workqueue);
2151};
2152
Michael S. Tsirkin181c04a2013-05-02 03:52:59 +03002153MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
2154MODULE_ALIAS("tcm_vhost");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002155MODULE_LICENSE("GPL");
2156module_init(tcm_vhost_init);
2157module_exit(tcm_vhost_exit);