blob: ea32b386797f5d52b70ee6f4028f5e8df43f3a8f [file] [log] [blame]
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001/*******************************************************************************
2 * Vhost kernel TCM fabric driver for virtio SCSI initiators
3 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07004 * (C) Copyright 2010-2013 Datera, Inc.
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07005 * (C) Copyright 2010-2012 IBM Corp.
6 *
7 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
8 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07009 * Authors: Nicholas A. Bellinger <nab@daterainc.com>
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070010 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 ****************************************************************************/
23
24#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <generated/utsrelease.h>
27#include <linux/utsname.h>
28#include <linux/init.h>
29#include <linux/slab.h>
30#include <linux/kthread.h>
31#include <linux/types.h>
32#include <linux/string.h>
33#include <linux/configfs.h>
34#include <linux/ctype.h>
35#include <linux/compat.h>
36#include <linux/eventfd.h>
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070037#include <linux/fs.h>
38#include <linux/miscdevice.h>
39#include <asm/unaligned.h>
40#include <scsi/scsi.h>
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070041#include <target/target_core_base.h>
42#include <target/target_core_fabric.h>
43#include <target/target_core_fabric_configfs.h>
44#include <target/target_core_configfs.h>
45#include <target/configfs_macros.h>
46#include <linux/vhost.h>
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070047#include <linux/virtio_scsi.h>
Asias He9d6064a2013-01-06 14:36:13 +080048#include <linux/llist.h>
Asias He1b7f3902013-02-06 13:20:59 +080049#include <linux/bitmap.h>
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -070050#include <linux/percpu_ida.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
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -080054#define VHOST_SCSI_VERSION "v0.1"
55#define VHOST_SCSI_NAMELEN 256
56#define VHOST_SCSI_MAX_CDB_SIZE 32
57#define VHOST_SCSI_DEFAULT_TAGS 256
58#define VHOST_SCSI_PREALLOC_SGLS 2048
59#define VHOST_SCSI_PREALLOC_UPAGES 2048
60#define VHOST_SCSI_PREALLOC_PROT_SGLS 512
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +030061
62struct vhost_scsi_inflight {
63 /* Wait for the flush operation to finish */
64 struct completion comp;
65 /* Refcount for the inflight reqs */
66 struct kref kref;
67};
68
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -080069struct vhost_scsi_cmd {
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +030070 /* Descriptor from vhost_get_vq_desc() for virt_queue segment */
71 int tvc_vq_desc;
72 /* virtio-scsi initiator task attribute */
73 int tvc_task_attr;
Nicholas Bellinger79c14142015-01-27 13:13:12 -080074 /* virtio-scsi response incoming iovecs */
75 int tvc_in_iovs;
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +030076 /* virtio-scsi initiator data direction */
77 enum dma_data_direction tvc_data_direction;
78 /* Expected data transfer length from virtio-scsi header */
79 u32 tvc_exp_data_len;
80 /* The Tag from include/linux/virtio_scsi.h:struct virtio_scsi_cmd_req */
81 u64 tvc_tag;
82 /* The number of scatterlists associated with this cmd */
83 u32 tvc_sgl_count;
Nicholas Bellingere31885d2014-02-22 18:34:43 -080084 u32 tvc_prot_sgl_count;
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -080085 /* Saved unpacked SCSI LUN for vhost_scsi_submission_work() */
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +030086 u32 tvc_lun;
87 /* Pointer to the SGL formatted memory from virtio-scsi */
88 struct scatterlist *tvc_sgl;
Nicholas Bellingerb1935f62014-02-22 18:08:24 -080089 struct scatterlist *tvc_prot_sgl;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -070090 struct page **tvc_upages;
Nicholas Bellinger79c14142015-01-27 13:13:12 -080091 /* Pointer to response header iovec */
92 struct iovec *tvc_resp_iov;
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +030093 /* Pointer to vhost_scsi for our device */
94 struct vhost_scsi *tvc_vhost;
95 /* Pointer to vhost_virtqueue for the cmd */
96 struct vhost_virtqueue *tvc_vq;
97 /* Pointer to vhost nexus memory */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -080098 struct vhost_scsi_nexus *tvc_nexus;
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +030099 /* The TCM I/O descriptor that is accessed via container_of() */
100 struct se_cmd tvc_se_cmd;
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800101 /* work item used for cmwq dispatch to vhost_scsi_submission_work() */
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300102 struct work_struct work;
103 /* Copy of the incoming SCSI command descriptor block (CDB) */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800104 unsigned char tvc_cdb[VHOST_SCSI_MAX_CDB_SIZE];
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300105 /* Sense buffer that will be mapped into outgoing status */
106 unsigned char tvc_sense_buf[TRANSPORT_SENSE_BUFFER];
107 /* Completed commands list, serviced from vhost worker thread */
108 struct llist_node tvc_completion_list;
109 /* Used to track inflight cmd */
110 struct vhost_scsi_inflight *inflight;
111};
112
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800113struct vhost_scsi_nexus {
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300114 /* Pointer to TCM session for I_T Nexus */
115 struct se_session *tvn_se_sess;
116};
117
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800118struct vhost_scsi_nacl {
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300119 /* Binary World Wide unique Port Name for Vhost Initiator port */
120 u64 iport_wwpn;
121 /* ASCII formatted WWPN for Sas Initiator port */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800122 char iport_name[VHOST_SCSI_NAMELEN];
123 /* Returned by vhost_scsi_make_nodeacl() */
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300124 struct se_node_acl se_node_acl;
125};
126
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800127struct vhost_scsi_tpg {
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300128 /* Vhost port target portal group tag for TCM */
129 u16 tport_tpgt;
130 /* Used to track number of TPG Port/Lun Links wrt to explict I_T Nexus shutdown */
131 int tv_tpg_port_count;
132 /* Used for vhost_scsi device reference to tpg_nexus, protected by tv_tpg_mutex */
133 int tv_tpg_vhost_count;
Nicholas Bellingerb1d75fe2015-03-28 00:03:51 -0700134 /* Used for enabling T10-PI with legacy devices */
135 int tv_fabric_prot_type;
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800136 /* list for vhost_scsi_list */
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300137 struct list_head tv_tpg_list;
138 /* Used to protect access for tpg_nexus */
139 struct mutex tv_tpg_mutex;
140 /* Pointer to the TCM VHost I_T Nexus for this TPG endpoint */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800141 struct vhost_scsi_nexus *tpg_nexus;
142 /* Pointer back to vhost_scsi_tport */
143 struct vhost_scsi_tport *tport;
144 /* Returned by vhost_scsi_make_tpg() */
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300145 struct se_portal_group se_tpg;
146 /* Pointer back to vhost_scsi, protected by tv_tpg_mutex */
147 struct vhost_scsi *vhost_scsi;
148};
149
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800150struct vhost_scsi_tport {
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300151 /* SCSI protocol the tport is providing */
152 u8 tport_proto_id;
153 /* Binary World Wide unique Port Name for Vhost Target port */
154 u64 tport_wwpn;
155 /* ASCII formatted WWPN for Vhost Target port */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800156 char tport_name[VHOST_SCSI_NAMELEN];
157 /* Returned by vhost_scsi_make_tport() */
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300158 struct se_wwn tport_wwn;
159};
160
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800161struct vhost_scsi_evt {
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300162 /* event to be sent to guest */
163 struct virtio_scsi_event event;
164 /* event list, serviced from vhost worker thread */
165 struct llist_node list;
166};
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700167
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700168enum {
169 VHOST_SCSI_VQ_CTL = 0,
170 VHOST_SCSI_VQ_EVT = 1,
171 VHOST_SCSI_VQ_IO = 2,
172};
173
Michael S. Tsirkine72fd722014-11-23 18:01:34 +0200174/* Note: can't set VIRTIO_F_VERSION_1 yet, since that implies ANY_LAYOUT. */
Nicholas Bellinger5dade712013-03-27 17:23:41 -0700175enum {
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800176 VHOST_SCSI_FEATURES = VHOST_FEATURES | (1ULL << VIRTIO_SCSI_F_HOTPLUG) |
Nicholas Bellinger664ed902015-01-28 01:31:52 -0800177 (1ULL << VIRTIO_SCSI_F_T10_PI) |
178 (1ULL << VIRTIO_F_ANY_LAYOUT) |
179 (1ULL << VIRTIO_F_VERSION_1)
Nicholas Bellinger5dade712013-03-27 17:23:41 -0700180};
181
Asias He1b7f3902013-02-06 13:20:59 +0800182#define VHOST_SCSI_MAX_TARGET 256
183#define VHOST_SCSI_MAX_VQ 128
Asias Hea6c9af82013-04-25 15:35:21 +0800184#define VHOST_SCSI_MAX_EVENT 128
Asias He67e18cf2013-02-05 12:31:57 +0800185
Asias He3ab2e422013-04-27 11:16:48 +0800186struct vhost_scsi_virtqueue {
187 struct vhost_virtqueue vq;
Michael S. Tsirkin3dfbff32013-04-28 15:38:52 +0300188 /*
189 * Reference counting for inflight reqs, used for flush operation. At
190 * each time, one reference tracks new commands submitted, while we
191 * wait for another one to reach 0.
192 */
Asias Hef2f0173d2013-04-27 11:16:49 +0800193 struct vhost_scsi_inflight inflights[2];
Michael S. Tsirkin3dfbff32013-04-28 15:38:52 +0300194 /*
195 * Indicate current inflight in use, protected by vq->mutex.
196 * Writers must also take dev mutex and flush under it.
197 */
Asias Hef2f0173d2013-04-27 11:16:49 +0800198 int inflight_idx;
Asias He3ab2e422013-04-27 11:16:48 +0800199};
200
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700201struct vhost_scsi {
Asias He67e18cf2013-02-05 12:31:57 +0800202 /* Protected by vhost_scsi->dev.mutex */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800203 struct vhost_scsi_tpg **vs_tpg;
Asias He67e18cf2013-02-05 12:31:57 +0800204 char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
Asias He67e18cf2013-02-05 12:31:57 +0800205
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700206 struct vhost_dev dev;
Asias He3ab2e422013-04-27 11:16:48 +0800207 struct vhost_scsi_virtqueue vqs[VHOST_SCSI_MAX_VQ];
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700208
209 struct vhost_work vs_completion_work; /* cmd completion work item */
Asias He9d6064a2013-01-06 14:36:13 +0800210 struct llist_head vs_completion_list; /* cmd completion queue */
Asias Hea6c9af82013-04-25 15:35:21 +0800211
212 struct vhost_work vs_event_work; /* evt injection work item */
213 struct llist_head vs_event_list; /* evt injection queue */
214
215 bool vs_events_missed; /* any missed events, protected by vq->mutex */
216 int vs_events_nr; /* num of pending events, protected by vq->mutex */
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700217};
218
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200219static struct target_core_fabric_ops vhost_scsi_ops;
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800220static struct workqueue_struct *vhost_scsi_workqueue;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700221
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800222/* Global spinlock to protect vhost_scsi TPG list for vhost IOCTL access */
223static DEFINE_MUTEX(vhost_scsi_mutex);
224static LIST_HEAD(vhost_scsi_list);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700225
Nicholas Bellingerb4078b52015-01-28 13:10:51 -0800226static int iov_num_pages(void __user *iov_base, size_t iov_len)
Asias He765b34f2013-01-22 11:20:25 +0800227{
Nicholas Bellingerb4078b52015-01-28 13:10:51 -0800228 return (PAGE_ALIGN((unsigned long)iov_base + iov_len) -
229 ((unsigned long)iov_base & PAGE_MASK)) >> PAGE_SHIFT;
Asias He765b34f2013-01-22 11:20:25 +0800230}
231
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800232static void vhost_scsi_done_inflight(struct kref *kref)
Asias Hef2f0173d2013-04-27 11:16:49 +0800233{
234 struct vhost_scsi_inflight *inflight;
235
236 inflight = container_of(kref, struct vhost_scsi_inflight, kref);
237 complete(&inflight->comp);
238}
239
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800240static void vhost_scsi_init_inflight(struct vhost_scsi *vs,
Asias Hef2f0173d2013-04-27 11:16:49 +0800241 struct vhost_scsi_inflight *old_inflight[])
242{
243 struct vhost_scsi_inflight *new_inflight;
244 struct vhost_virtqueue *vq;
245 int idx, i;
246
247 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
248 vq = &vs->vqs[i].vq;
249
250 mutex_lock(&vq->mutex);
251
252 /* store old infight */
253 idx = vs->vqs[i].inflight_idx;
254 if (old_inflight)
255 old_inflight[i] = &vs->vqs[i].inflights[idx];
256
257 /* setup new infight */
258 vs->vqs[i].inflight_idx = idx ^ 1;
259 new_inflight = &vs->vqs[i].inflights[idx ^ 1];
260 kref_init(&new_inflight->kref);
261 init_completion(&new_inflight->comp);
262
263 mutex_unlock(&vq->mutex);
264 }
265}
266
267static struct vhost_scsi_inflight *
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800268vhost_scsi_get_inflight(struct vhost_virtqueue *vq)
Asias Hef2f0173d2013-04-27 11:16:49 +0800269{
270 struct vhost_scsi_inflight *inflight;
271 struct vhost_scsi_virtqueue *svq;
272
273 svq = container_of(vq, struct vhost_scsi_virtqueue, vq);
274 inflight = &svq->inflights[svq->inflight_idx];
275 kref_get(&inflight->kref);
276
277 return inflight;
278}
279
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800280static void vhost_scsi_put_inflight(struct vhost_scsi_inflight *inflight)
Asias Hef2f0173d2013-04-27 11:16:49 +0800281{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800282 kref_put(&inflight->kref, vhost_scsi_done_inflight);
Asias Hef2f0173d2013-04-27 11:16:49 +0800283}
284
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800285static int vhost_scsi_check_true(struct se_portal_group *se_tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700286{
287 return 1;
288}
289
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800290static int vhost_scsi_check_false(struct se_portal_group *se_tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700291{
292 return 0;
293}
294
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800295static char *vhost_scsi_get_fabric_name(void)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700296{
297 return "vhost";
298}
299
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800300static u8 vhost_scsi_get_fabric_proto_ident(struct se_portal_group *se_tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700301{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800302 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
303 struct vhost_scsi_tpg, se_tpg);
304 struct vhost_scsi_tport *tport = tpg->tport;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700305
306 switch (tport->tport_proto_id) {
307 case SCSI_PROTOCOL_SAS:
308 return sas_get_fabric_proto_ident(se_tpg);
309 case SCSI_PROTOCOL_FCP:
310 return fc_get_fabric_proto_ident(se_tpg);
311 case SCSI_PROTOCOL_ISCSI:
312 return iscsi_get_fabric_proto_ident(se_tpg);
313 default:
314 pr_err("Unknown tport_proto_id: 0x%02x, using"
315 " SAS emulation\n", tport->tport_proto_id);
316 break;
317 }
318
319 return sas_get_fabric_proto_ident(se_tpg);
320}
321
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800322static char *vhost_scsi_get_fabric_wwn(struct se_portal_group *se_tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700323{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800324 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
325 struct vhost_scsi_tpg, se_tpg);
326 struct vhost_scsi_tport *tport = tpg->tport;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700327
328 return &tport->tport_name[0];
329}
330
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800331static u16 vhost_scsi_get_tpgt(struct se_portal_group *se_tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700332{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800333 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
334 struct vhost_scsi_tpg, se_tpg);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700335 return tpg->tport_tpgt;
336}
337
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800338static u32 vhost_scsi_get_default_depth(struct se_portal_group *se_tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700339{
340 return 1;
341}
342
Asias He683bd962013-05-06 16:38:27 +0800343static u32
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800344vhost_scsi_get_pr_transport_id(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +0800345 struct se_node_acl *se_nacl,
346 struct t10_pr_registration *pr_reg,
347 int *format_code,
348 unsigned char *buf)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700349{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800350 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
351 struct vhost_scsi_tpg, se_tpg);
352 struct vhost_scsi_tport *tport = tpg->tport;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700353
354 switch (tport->tport_proto_id) {
355 case SCSI_PROTOCOL_SAS:
356 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
357 format_code, buf);
358 case SCSI_PROTOCOL_FCP:
359 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
360 format_code, buf);
361 case SCSI_PROTOCOL_ISCSI:
362 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
363 format_code, buf);
364 default:
365 pr_err("Unknown tport_proto_id: 0x%02x, using"
366 " SAS emulation\n", tport->tport_proto_id);
367 break;
368 }
369
370 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
371 format_code, buf);
372}
373
Asias He683bd962013-05-06 16:38:27 +0800374static u32
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800375vhost_scsi_get_pr_transport_id_len(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +0800376 struct se_node_acl *se_nacl,
377 struct t10_pr_registration *pr_reg,
378 int *format_code)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700379{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800380 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
381 struct vhost_scsi_tpg, se_tpg);
382 struct vhost_scsi_tport *tport = tpg->tport;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700383
384 switch (tport->tport_proto_id) {
385 case SCSI_PROTOCOL_SAS:
386 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
387 format_code);
388 case SCSI_PROTOCOL_FCP:
389 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
390 format_code);
391 case SCSI_PROTOCOL_ISCSI:
392 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
393 format_code);
394 default:
395 pr_err("Unknown tport_proto_id: 0x%02x, using"
396 " SAS emulation\n", tport->tport_proto_id);
397 break;
398 }
399
400 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
401 format_code);
402}
403
Asias He683bd962013-05-06 16:38:27 +0800404static char *
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800405vhost_scsi_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +0800406 const char *buf,
407 u32 *out_tid_len,
408 char **port_nexus_ptr)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700409{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800410 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
411 struct vhost_scsi_tpg, se_tpg);
412 struct vhost_scsi_tport *tport = tpg->tport;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700413
414 switch (tport->tport_proto_id) {
415 case SCSI_PROTOCOL_SAS:
416 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
417 port_nexus_ptr);
418 case SCSI_PROTOCOL_FCP:
419 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
420 port_nexus_ptr);
421 case SCSI_PROTOCOL_ISCSI:
422 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
423 port_nexus_ptr);
424 default:
425 pr_err("Unknown tport_proto_id: 0x%02x, using"
426 " SAS emulation\n", tport->tport_proto_id);
427 break;
428 }
429
430 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
431 port_nexus_ptr);
432}
433
Nicholas Bellingerb1d75fe2015-03-28 00:03:51 -0700434static int vhost_scsi_check_prot_fabric_only(struct se_portal_group *se_tpg)
435{
436 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
437 struct vhost_scsi_tpg, se_tpg);
438
439 return tpg->tv_fabric_prot_type;
440}
441
Asias He683bd962013-05-06 16:38:27 +0800442static struct se_node_acl *
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800443vhost_scsi_alloc_fabric_acl(struct se_portal_group *se_tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700444{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800445 struct vhost_scsi_nacl *nacl;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700446
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800447 nacl = kzalloc(sizeof(struct vhost_scsi_nacl), GFP_KERNEL);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700448 if (!nacl) {
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800449 pr_err("Unable to allocate struct vhost_scsi_nacl\n");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700450 return NULL;
451 }
452
453 return &nacl->se_node_acl;
454}
455
Asias He683bd962013-05-06 16:38:27 +0800456static void
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800457vhost_scsi_release_fabric_acl(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +0800458 struct se_node_acl *se_nacl)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700459{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800460 struct vhost_scsi_nacl *nacl = container_of(se_nacl,
461 struct vhost_scsi_nacl, se_node_acl);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700462 kfree(nacl);
463}
464
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800465static u32 vhost_scsi_tpg_get_inst_index(struct se_portal_group *se_tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700466{
467 return 1;
468}
469
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800470static void vhost_scsi_release_cmd(struct se_cmd *se_cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700471{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800472 struct vhost_scsi_cmd *tv_cmd = container_of(se_cmd,
473 struct vhost_scsi_cmd, tvc_se_cmd);
Nicholas Bellingerde1419e2015-01-29 17:21:13 -0800474 struct se_session *se_sess = tv_cmd->tvc_nexus->tvn_se_sess;
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800475 int i;
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700476
477 if (tv_cmd->tvc_sgl_count) {
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700478 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
479 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
Michael S. Tsirkind3d665a2013-09-17 22:54:31 +0300480 }
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800481 if (tv_cmd->tvc_prot_sgl_count) {
482 for (i = 0; i < tv_cmd->tvc_prot_sgl_count; i++)
483 put_page(sg_page(&tv_cmd->tvc_prot_sgl[i]));
484 }
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700485
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800486 vhost_scsi_put_inflight(tv_cmd->inflight);
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700487 percpu_ida_free(&se_sess->sess_tag_pool, se_cmd->map_tag);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700488}
489
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800490static int vhost_scsi_shutdown_session(struct se_session *se_sess)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700491{
492 return 0;
493}
494
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800495static void vhost_scsi_close_session(struct se_session *se_sess)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700496{
497 return;
498}
499
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800500static u32 vhost_scsi_sess_get_index(struct se_session *se_sess)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700501{
502 return 0;
503}
504
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800505static int vhost_scsi_write_pending(struct se_cmd *se_cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700506{
507 /* Go ahead and process the write immediately */
508 target_execute_cmd(se_cmd);
509 return 0;
510}
511
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800512static int vhost_scsi_write_pending_status(struct se_cmd *se_cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700513{
514 return 0;
515}
516
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800517static void vhost_scsi_set_default_node_attrs(struct se_node_acl *nacl)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700518{
519 return;
520}
521
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800522static u32 vhost_scsi_get_task_tag(struct se_cmd *se_cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700523{
524 return 0;
525}
526
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800527static int vhost_scsi_get_cmd_state(struct se_cmd *se_cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700528{
529 return 0;
530}
531
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800532static void vhost_scsi_complete_cmd(struct vhost_scsi_cmd *cmd)
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700533{
Asias He3c63f662013-05-06 16:38:29 +0800534 struct vhost_scsi *vs = cmd->tvc_vhost;
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700535
Asias He3c63f662013-05-06 16:38:29 +0800536 llist_add(&cmd->tvc_completion_list, &vs->vs_completion_list);
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700537
538 vhost_work_queue(&vs->dev, &vs->vs_completion_work);
539}
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700540
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800541static int vhost_scsi_queue_data_in(struct se_cmd *se_cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700542{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800543 struct vhost_scsi_cmd *cmd = container_of(se_cmd,
544 struct vhost_scsi_cmd, tvc_se_cmd);
Asias He3c63f662013-05-06 16:38:29 +0800545 vhost_scsi_complete_cmd(cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700546 return 0;
547}
548
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800549static int vhost_scsi_queue_status(struct se_cmd *se_cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700550{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800551 struct vhost_scsi_cmd *cmd = container_of(se_cmd,
552 struct vhost_scsi_cmd, tvc_se_cmd);
Asias He3c63f662013-05-06 16:38:29 +0800553 vhost_scsi_complete_cmd(cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700554 return 0;
555}
556
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800557static void vhost_scsi_queue_tm_rsp(struct se_cmd *se_cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700558{
Joern Engelb79fafa2013-07-03 11:22:17 -0400559 return;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700560}
561
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800562static void vhost_scsi_aborted_task(struct se_cmd *se_cmd)
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700563{
564 return;
565}
566
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800567static void vhost_scsi_free_evt(struct vhost_scsi *vs, struct vhost_scsi_evt *evt)
Asias Hea6c9af82013-04-25 15:35:21 +0800568{
569 vs->vs_events_nr--;
570 kfree(evt);
571}
572
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800573static struct vhost_scsi_evt *
574vhost_scsi_allocate_evt(struct vhost_scsi *vs,
Asias He683bd962013-05-06 16:38:27 +0800575 u32 event, u32 reason)
Asias Hea6c9af82013-04-25 15:35:21 +0800576{
Asias He3ab2e422013-04-27 11:16:48 +0800577 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800578 struct vhost_scsi_evt *evt;
Asias Hea6c9af82013-04-25 15:35:21 +0800579
580 if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) {
581 vs->vs_events_missed = true;
582 return NULL;
583 }
584
585 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
586 if (!evt) {
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800587 vq_err(vq, "Failed to allocate vhost_scsi_evt\n");
Asias Hea6c9af82013-04-25 15:35:21 +0800588 vs->vs_events_missed = true;
589 return NULL;
590 }
591
Michael S. Tsirkine72fd722014-11-23 18:01:34 +0200592 evt->event.event = cpu_to_vhost32(vq, event);
593 evt->event.reason = cpu_to_vhost32(vq, reason);
Asias Hea6c9af82013-04-25 15:35:21 +0800594 vs->vs_events_nr++;
595
596 return evt;
597}
598
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800599static void vhost_scsi_free_cmd(struct vhost_scsi_cmd *cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700600{
Asias He3c63f662013-05-06 16:38:29 +0800601 struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700602
603 /* TODO locking against target/backend threads? */
Nicholas Bellinger6c131d02013-06-06 01:44:48 -0700604 transport_generic_free_cmd(se_cmd, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700605
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700606}
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700607
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700608static int vhost_scsi_check_stop_free(struct se_cmd *se_cmd)
609{
610 return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700611}
612
Asias He683bd962013-05-06 16:38:27 +0800613static void
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800614vhost_scsi_do_evt_work(struct vhost_scsi *vs, struct vhost_scsi_evt *evt)
Asias Hea6c9af82013-04-25 15:35:21 +0800615{
Asias He3ab2e422013-04-27 11:16:48 +0800616 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Asias Hea6c9af82013-04-25 15:35:21 +0800617 struct virtio_scsi_event *event = &evt->event;
618 struct virtio_scsi_event __user *eventp;
619 unsigned out, in;
620 int head, ret;
621
622 if (!vq->private_data) {
623 vs->vs_events_missed = true;
624 return;
625 }
626
627again:
628 vhost_disable_notify(&vs->dev, vq);
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300629 head = vhost_get_vq_desc(vq, vq->iov,
Asias Hea6c9af82013-04-25 15:35:21 +0800630 ARRAY_SIZE(vq->iov), &out, &in,
631 NULL, NULL);
632 if (head < 0) {
633 vs->vs_events_missed = true;
634 return;
635 }
636 if (head == vq->num) {
637 if (vhost_enable_notify(&vs->dev, vq))
638 goto again;
639 vs->vs_events_missed = true;
640 return;
641 }
642
643 if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) {
644 vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n",
645 vq->iov[out].iov_len);
646 vs->vs_events_missed = true;
647 return;
648 }
649
650 if (vs->vs_events_missed) {
Michael S. Tsirkine72fd722014-11-23 18:01:34 +0200651 event->event |= cpu_to_vhost32(vq, VIRTIO_SCSI_T_EVENTS_MISSED);
Asias Hea6c9af82013-04-25 15:35:21 +0800652 vs->vs_events_missed = false;
653 }
654
655 eventp = vq->iov[out].iov_base;
656 ret = __copy_to_user(eventp, event, sizeof(*event));
657 if (!ret)
658 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
659 else
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800660 vq_err(vq, "Faulted on vhost_scsi_send_event\n");
Asias Hea6c9af82013-04-25 15:35:21 +0800661}
662
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800663static void vhost_scsi_evt_work(struct vhost_work *work)
Asias Hea6c9af82013-04-25 15:35:21 +0800664{
665 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
666 vs_event_work);
Asias He3ab2e422013-04-27 11:16:48 +0800667 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800668 struct vhost_scsi_evt *evt;
Asias Hea6c9af82013-04-25 15:35:21 +0800669 struct llist_node *llnode;
670
671 mutex_lock(&vq->mutex);
672 llnode = llist_del_all(&vs->vs_event_list);
673 while (llnode) {
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800674 evt = llist_entry(llnode, struct vhost_scsi_evt, list);
Asias Hea6c9af82013-04-25 15:35:21 +0800675 llnode = llist_next(llnode);
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800676 vhost_scsi_do_evt_work(vs, evt);
677 vhost_scsi_free_evt(vs, evt);
Asias Hea6c9af82013-04-25 15:35:21 +0800678 }
679 mutex_unlock(&vq->mutex);
680}
681
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700682/* Fill in status and signal that we are done processing this command
683 *
684 * This is scheduled in the vhost work queue so we are called with the owner
685 * process mm and can access the vring.
686 */
687static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
688{
689 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
690 vs_completion_work);
Asias He1b7f3902013-02-06 13:20:59 +0800691 DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
Asias He9d6064a2013-01-06 14:36:13 +0800692 struct virtio_scsi_cmd_resp v_rsp;
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800693 struct vhost_scsi_cmd *cmd;
Asias He9d6064a2013-01-06 14:36:13 +0800694 struct llist_node *llnode;
695 struct se_cmd *se_cmd;
Nicholas Bellinger79c14142015-01-27 13:13:12 -0800696 struct iov_iter iov_iter;
Asias He1b7f3902013-02-06 13:20:59 +0800697 int ret, vq;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700698
Asias He1b7f3902013-02-06 13:20:59 +0800699 bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
Asias He9d6064a2013-01-06 14:36:13 +0800700 llnode = llist_del_all(&vs->vs_completion_list);
701 while (llnode) {
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800702 cmd = llist_entry(llnode, struct vhost_scsi_cmd,
Asias He9d6064a2013-01-06 14:36:13 +0800703 tvc_completion_list);
704 llnode = llist_next(llnode);
Asias He3c63f662013-05-06 16:38:29 +0800705 se_cmd = &cmd->tvc_se_cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700706
707 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
Asias He3c63f662013-05-06 16:38:29 +0800708 cmd, se_cmd->residual_count, se_cmd->scsi_status);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700709
710 memset(&v_rsp, 0, sizeof(v_rsp));
Michael S. Tsirkine72fd722014-11-23 18:01:34 +0200711 v_rsp.resid = cpu_to_vhost32(cmd->tvc_vq, se_cmd->residual_count);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700712 /* TODO is status_qualifier field needed? */
713 v_rsp.status = se_cmd->scsi_status;
Michael S. Tsirkine72fd722014-11-23 18:01:34 +0200714 v_rsp.sense_len = cpu_to_vhost32(cmd->tvc_vq,
715 se_cmd->scsi_sense_length);
Asias He3c63f662013-05-06 16:38:29 +0800716 memcpy(v_rsp.sense, cmd->tvc_sense_buf,
Michael S. Tsirkine72fd722014-11-23 18:01:34 +0200717 se_cmd->scsi_sense_length);
Nicholas Bellinger79c14142015-01-27 13:13:12 -0800718
719 iov_iter_init(&iov_iter, READ, cmd->tvc_resp_iov,
720 cmd->tvc_in_iovs, sizeof(v_rsp));
721 ret = copy_to_iter(&v_rsp, sizeof(v_rsp), &iov_iter);
722 if (likely(ret == sizeof(v_rsp))) {
Asias He3ab2e422013-04-27 11:16:48 +0800723 struct vhost_scsi_virtqueue *q;
Asias He3c63f662013-05-06 16:38:29 +0800724 vhost_add_used(cmd->tvc_vq, cmd->tvc_vq_desc, 0);
725 q = container_of(cmd->tvc_vq, struct vhost_scsi_virtqueue, vq);
Asias He3ab2e422013-04-27 11:16:48 +0800726 vq = q - vs->vqs;
Asias He1b7f3902013-02-06 13:20:59 +0800727 __set_bit(vq, signal);
728 } else
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700729 pr_err("Faulted on virtio_scsi_cmd_resp\n");
730
Asias He3c63f662013-05-06 16:38:29 +0800731 vhost_scsi_free_cmd(cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700732 }
733
Asias He1b7f3902013-02-06 13:20:59 +0800734 vq = -1;
735 while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1))
736 < VHOST_SCSI_MAX_VQ)
Asias He3ab2e422013-04-27 11:16:48 +0800737 vhost_signal(&vs->dev, &vs->vqs[vq].vq);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700738}
739
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800740static struct vhost_scsi_cmd *
741vhost_scsi_get_tag(struct vhost_virtqueue *vq, struct vhost_scsi_tpg *tpg,
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800742 unsigned char *cdb, u64 scsi_tag, u16 lun, u8 task_attr,
743 u32 exp_data_len, int data_direction)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700744{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800745 struct vhost_scsi_cmd *cmd;
746 struct vhost_scsi_nexus *tv_nexus;
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700747 struct se_session *se_sess;
Nicholas Bellingerb1935f62014-02-22 18:08:24 -0800748 struct scatterlist *sg, *prot_sg;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700749 struct page **pages;
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700750 int tag;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700751
Asias He98718312013-05-06 16:38:28 +0800752 tv_nexus = tpg->tpg_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700753 if (!tv_nexus) {
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800754 pr_err("Unable to locate active struct vhost_scsi_nexus\n");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700755 return ERR_PTR(-EIO);
756 }
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700757 se_sess = tv_nexus->tvn_se_sess;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700758
Kent Overstreet6f6b5d12014-01-19 08:26:37 +0000759 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
Nicholas Bellinger4a47d3a2013-09-23 11:42:28 -0700760 if (tag < 0) {
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800761 pr_err("Unable to obtain tag for vhost_scsi_cmd\n");
Nicholas Bellinger4a47d3a2013-09-23 11:42:28 -0700762 return ERR_PTR(-ENOMEM);
763 }
764
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800765 cmd = &((struct vhost_scsi_cmd *)se_sess->sess_cmd_map)[tag];
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700766 sg = cmd->tvc_sgl;
Nicholas Bellingerb1935f62014-02-22 18:08:24 -0800767 prot_sg = cmd->tvc_prot_sgl;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700768 pages = cmd->tvc_upages;
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800769 memset(cmd, 0, sizeof(struct vhost_scsi_cmd));
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700770
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700771 cmd->tvc_sgl = sg;
Nicholas Bellingerb1935f62014-02-22 18:08:24 -0800772 cmd->tvc_prot_sgl = prot_sg;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700773 cmd->tvc_upages = pages;
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700774 cmd->tvc_se_cmd.map_tag = tag;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800775 cmd->tvc_tag = scsi_tag;
776 cmd->tvc_lun = lun;
777 cmd->tvc_task_attr = task_attr;
Asias He3c63f662013-05-06 16:38:29 +0800778 cmd->tvc_exp_data_len = exp_data_len;
779 cmd->tvc_data_direction = data_direction;
780 cmd->tvc_nexus = tv_nexus;
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800781 cmd->inflight = vhost_scsi_get_inflight(vq);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700782
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800783 memcpy(cmd->tvc_cdb, cdb, VHOST_SCSI_MAX_CDB_SIZE);
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800784
Asias He3c63f662013-05-06 16:38:29 +0800785 return cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700786}
787
788/*
789 * Map a user memory range into a scatterlist
790 *
791 * Returns the number of scatterlist entries used or -errno on error.
792 */
Asias He683bd962013-05-06 16:38:27 +0800793static int
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800794vhost_scsi_map_to_sgl(struct vhost_scsi_cmd *cmd,
Nicholas Bellingerb4078b52015-01-28 13:10:51 -0800795 void __user *ptr,
796 size_t len,
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700797 struct scatterlist *sgl,
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800798 bool write)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700799{
Nicholas Bellingerb4078b52015-01-28 13:10:51 -0800800 unsigned int npages = 0, offset, nbytes;
801 unsigned int pages_nr = iov_num_pages(ptr, len);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700802 struct scatterlist *sg = sgl;
Nicholas Bellingerb4078b52015-01-28 13:10:51 -0800803 struct page **pages = cmd->tvc_upages;
Asias He18100532013-01-22 11:20:27 +0800804 int ret, i;
805
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800806 if (pages_nr > VHOST_SCSI_PREALLOC_UPAGES) {
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700807 pr_err("vhost_scsi_map_to_sgl() pages_nr: %u greater than"
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800808 " preallocated VHOST_SCSI_PREALLOC_UPAGES: %u\n",
809 pages_nr, VHOST_SCSI_PREALLOC_UPAGES);
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800810 return -ENOBUFS;
811 }
Asias He18100532013-01-22 11:20:27 +0800812
813 ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages);
814 /* No pages were pinned */
815 if (ret < 0)
816 goto out;
817 /* Less pages pinned than wanted */
818 if (ret != pages_nr) {
819 for (i = 0; i < ret; i++)
820 put_page(pages[i]);
821 ret = -EFAULT;
822 goto out;
823 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700824
825 while (len > 0) {
Asias He18100532013-01-22 11:20:27 +0800826 offset = (uintptr_t)ptr & ~PAGE_MASK;
827 nbytes = min_t(unsigned int, PAGE_SIZE - offset, len);
828 sg_set_page(sg, pages[npages], nbytes, offset);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700829 ptr += nbytes;
830 len -= nbytes;
831 sg++;
832 npages++;
833 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700834
Asias He18100532013-01-22 11:20:27 +0800835out:
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700836 return ret;
837}
838
Asias He683bd962013-05-06 16:38:27 +0800839static int
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800840vhost_scsi_calc_sgls(struct iov_iter *iter, size_t bytes, int max_sgls)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700841{
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800842 int sgl_count = 0;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700843
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800844 if (!iter || !iter->iov) {
845 pr_err("%s: iter->iov is NULL, but expected bytes: %zu"
846 " present\n", __func__, bytes);
847 return -EINVAL;
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800848 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700849
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800850 sgl_count = iov_iter_npages(iter, 0xffff);
851 if (sgl_count > max_sgls) {
852 pr_err("%s: requested sgl_count: %d exceeds pre-allocated"
853 " max_sgls: %d\n", __func__, sgl_count, max_sgls);
854 return -EINVAL;
855 }
856 return sgl_count;
857}
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700858
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800859static int
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800860vhost_scsi_iov_to_sgl(struct vhost_scsi_cmd *cmd, bool write,
861 struct iov_iter *iter,
862 struct scatterlist *sg, int sg_count)
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800863{
864 size_t off = iter->iov_offset;
865 int i, ret;
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800866
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800867 for (i = 0; i < iter->nr_segs; i++) {
868 void __user *base = iter->iov[i].iov_base + off;
869 size_t len = iter->iov[i].iov_len - off;
870
871 ret = vhost_scsi_map_to_sgl(cmd, base, len, sg, write);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700872 if (ret < 0) {
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800873 for (i = 0; i < sg_count; i++) {
874 struct page *page = sg_page(&sg[i]);
875 if (page)
876 put_page(page);
877 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700878 return ret;
879 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700880 sg += ret;
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800881 off = 0;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700882 }
883 return 0;
884}
885
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800886static int
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800887vhost_scsi_mapal(struct vhost_scsi_cmd *cmd,
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800888 size_t prot_bytes, struct iov_iter *prot_iter,
889 size_t data_bytes, struct iov_iter *data_iter)
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800890{
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800891 int sgl_count, ret;
892 bool write = (cmd->tvc_data_direction == DMA_FROM_DEVICE);
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800893
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800894 if (prot_bytes) {
895 sgl_count = vhost_scsi_calc_sgls(prot_iter, prot_bytes,
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800896 VHOST_SCSI_PREALLOC_PROT_SGLS);
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800897 if (sgl_count < 0)
898 return sgl_count;
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800899
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800900 sg_init_table(cmd->tvc_prot_sgl, sgl_count);
901 cmd->tvc_prot_sgl_count = sgl_count;
902 pr_debug("%s prot_sg %p prot_sgl_count %u\n", __func__,
903 cmd->tvc_prot_sgl, cmd->tvc_prot_sgl_count);
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800904
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800905 ret = vhost_scsi_iov_to_sgl(cmd, write, prot_iter,
906 cmd->tvc_prot_sgl,
907 cmd->tvc_prot_sgl_count);
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800908 if (ret < 0) {
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800909 cmd->tvc_prot_sgl_count = 0;
910 return ret;
911 }
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800912 }
913 sgl_count = vhost_scsi_calc_sgls(data_iter, data_bytes,
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800914 VHOST_SCSI_PREALLOC_SGLS);
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800915 if (sgl_count < 0)
916 return sgl_count;
917
918 sg_init_table(cmd->tvc_sgl, sgl_count);
919 cmd->tvc_sgl_count = sgl_count;
920 pr_debug("%s data_sg %p data_sgl_count %u\n", __func__,
921 cmd->tvc_sgl, cmd->tvc_sgl_count);
922
923 ret = vhost_scsi_iov_to_sgl(cmd, write, data_iter,
924 cmd->tvc_sgl, cmd->tvc_sgl_count);
925 if (ret < 0) {
926 cmd->tvc_sgl_count = 0;
927 return ret;
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800928 }
929 return 0;
930}
931
Nicholas Bellinger46243862014-12-21 10:42:08 -0800932static int vhost_scsi_to_tcm_attr(int attr)
933{
934 switch (attr) {
935 case VIRTIO_SCSI_S_SIMPLE:
936 return TCM_SIMPLE_TAG;
937 case VIRTIO_SCSI_S_ORDERED:
938 return TCM_ORDERED_TAG;
939 case VIRTIO_SCSI_S_HEAD:
940 return TCM_HEAD_TAG;
941 case VIRTIO_SCSI_S_ACA:
942 return TCM_ACA_TAG;
943 default:
944 break;
945 }
946 return TCM_SIMPLE_TAG;
947}
948
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800949static void vhost_scsi_submission_work(struct work_struct *work)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700950{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800951 struct vhost_scsi_cmd *cmd =
952 container_of(work, struct vhost_scsi_cmd, work);
953 struct vhost_scsi_nexus *tv_nexus;
Asias He3c63f662013-05-06 16:38:29 +0800954 struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800955 struct scatterlist *sg_ptr, *sg_prot_ptr = NULL;
956 int rc;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700957
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800958 /* FIXME: BIDI operation */
Asias He3c63f662013-05-06 16:38:29 +0800959 if (cmd->tvc_sgl_count) {
960 sg_ptr = cmd->tvc_sgl;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800961
962 if (cmd->tvc_prot_sgl_count)
963 sg_prot_ptr = cmd->tvc_prot_sgl;
964 else
965 se_cmd->prot_pto = true;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700966 } else {
967 sg_ptr = NULL;
968 }
Asias He3c63f662013-05-06 16:38:29 +0800969 tv_nexus = cmd->tvc_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700970
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700971 rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
Asias He3c63f662013-05-06 16:38:29 +0800972 cmd->tvc_cdb, &cmd->tvc_sense_buf[0],
973 cmd->tvc_lun, cmd->tvc_exp_data_len,
Nicholas Bellinger46243862014-12-21 10:42:08 -0800974 vhost_scsi_to_tcm_attr(cmd->tvc_task_attr),
975 cmd->tvc_data_direction, TARGET_SCF_ACK_KREF,
976 sg_ptr, cmd->tvc_sgl_count, NULL, 0, sg_prot_ptr,
977 cmd->tvc_prot_sgl_count);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700978 if (rc < 0) {
979 transport_send_check_condition_and_sense(se_cmd,
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700980 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700981 transport_generic_free_cmd(se_cmd, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700982 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700983}
984
Asias He683bd962013-05-06 16:38:27 +0800985static void
986vhost_scsi_send_bad_target(struct vhost_scsi *vs,
987 struct vhost_virtqueue *vq,
988 int head, unsigned out)
Asias He637ab212013-04-10 15:06:15 +0800989{
990 struct virtio_scsi_cmd_resp __user *resp;
991 struct virtio_scsi_cmd_resp rsp;
992 int ret;
993
994 memset(&rsp, 0, sizeof(rsp));
995 rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
996 resp = vq->iov[out].iov_base;
997 ret = __copy_to_user(resp, &rsp, sizeof(rsp));
998 if (!ret)
999 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
1000 else
1001 pr_err("Faulted on virtio_scsi_cmd_resp\n");
1002}
1003
Asias He683bd962013-05-06 16:38:27 +08001004static void
1005vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001006{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001007 struct vhost_scsi_tpg **vs_tpg, *tpg;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001008 struct virtio_scsi_cmd_req v_req;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001009 struct virtio_scsi_cmd_req_pi v_req_pi;
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001010 struct vhost_scsi_cmd *cmd;
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001011 struct iov_iter out_iter, in_iter, prot_iter, data_iter;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001012 u64 tag;
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001013 u32 exp_data_len, data_direction;
1014 unsigned out, in;
1015 int head, ret, prot_bytes;
1016 size_t req_size, rsp_size = sizeof(struct virtio_scsi_cmd_resp);
1017 size_t out_size, in_size;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001018 u16 lun;
1019 u8 *target, *lunp, task_attr;
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001020 bool t10_pi = vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI);
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001021 void *req, *cdb;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001022
Asias Hee7802212013-05-07 14:54:35 +08001023 mutex_lock(&vq->mutex);
Asias He4f7f46d2013-04-03 14:17:37 +08001024 /*
1025 * We can handle the vq only after the endpoint is setup by calling the
1026 * VHOST_SCSI_SET_ENDPOINT ioctl.
Asias He4f7f46d2013-04-03 14:17:37 +08001027 */
Asias Hee7802212013-05-07 14:54:35 +08001028 vs_tpg = vq->private_data;
Asias He4f7f46d2013-04-03 14:17:37 +08001029 if (!vs_tpg)
Asias Hee7802212013-05-07 14:54:35 +08001030 goto out;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001031
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001032 vhost_disable_notify(&vs->dev, vq);
1033
1034 for (;;) {
Michael S. Tsirkin47283be2014-06-05 15:20:27 +03001035 head = vhost_get_vq_desc(vq, vq->iov,
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001036 ARRAY_SIZE(vq->iov), &out, &in,
1037 NULL, NULL);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001038 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001039 head, out, in);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001040 /* On error, stop handling until the next kick. */
1041 if (unlikely(head < 0))
1042 break;
1043 /* Nothing new? Wait for eventfd to tell us they refilled. */
1044 if (head == vq->num) {
1045 if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
1046 vhost_disable_notify(&vs->dev, vq);
1047 continue;
1048 }
1049 break;
1050 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001051 /*
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001052 * Check for a sane response buffer so we can report early
1053 * errors back to the guest.
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001054 */
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001055 if (unlikely(vq->iov[out].iov_len < rsp_size)) {
1056 vq_err(vq, "Expecting at least virtio_scsi_cmd_resp"
1057 " size, got %zu bytes\n", vq->iov[out].iov_len);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001058 break;
1059 }
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001060 /*
1061 * Setup pointers and values based upon different virtio-scsi
1062 * request header if T10_PI is enabled in KVM guest.
1063 */
1064 if (t10_pi) {
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001065 req = &v_req_pi;
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001066 req_size = sizeof(v_req_pi);
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001067 lunp = &v_req_pi.lun[0];
1068 target = &v_req_pi.lun[1];
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001069 } else {
1070 req = &v_req;
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001071 req_size = sizeof(v_req);
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001072 lunp = &v_req.lun[0];
1073 target = &v_req.lun[1];
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001074 }
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001075 /*
1076 * FIXME: Not correct for BIDI operation
1077 */
1078 out_size = iov_length(vq->iov, out);
1079 in_size = iov_length(&vq->iov[out], in);
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001080
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001081 /*
1082 * Copy over the virtio-scsi request header, which for a
1083 * ANY_LAYOUT enabled guest may span multiple iovecs, or a
1084 * single iovec may contain both the header + outgoing
1085 * WRITE payloads.
1086 *
1087 * copy_from_iter() will advance out_iter, so that it will
1088 * point at the start of the outgoing WRITE payload, if
1089 * DMA_TO_DEVICE is set.
1090 */
1091 iov_iter_init(&out_iter, WRITE, vq->iov, out, out_size);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001092
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001093 ret = copy_from_iter(req, req_size, &out_iter);
1094 if (unlikely(ret != req_size)) {
1095 vq_err(vq, "Faulted on copy_from_iter\n");
Nicholas Bellingerde1419e2015-01-29 17:21:13 -08001096 vhost_scsi_send_bad_target(vs, vq, head, out);
1097 continue;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001098 }
Venkatesh Srinivas7fe412d2014-02-24 14:13:32 -08001099 /* virtio-scsi spec requires byte 0 of the lun to be 1 */
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001100 if (unlikely(*lunp != 1)) {
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001101 vq_err(vq, "Illegal virtio-scsi lun: %u\n", *lunp);
Venkatesh Srinivas7fe412d2014-02-24 14:13:32 -08001102 vhost_scsi_send_bad_target(vs, vq, head, out);
1103 continue;
1104 }
1105
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001106 tpg = ACCESS_ONCE(vs_tpg[*target]);
Asias He98718312013-05-06 16:38:28 +08001107 if (unlikely(!tpg)) {
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001108 /* Target does not exist, fail the request */
Asias He637ab212013-04-10 15:06:15 +08001109 vhost_scsi_send_bad_target(vs, vq, head, out);
Asias He67e18cf2013-02-05 12:31:57 +08001110 continue;
1111 }
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001112 /*
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001113 * Determine data_direction by calculating the total outgoing
1114 * iovec sizes + incoming iovec sizes vs. virtio-scsi request +
1115 * response headers respectively.
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001116 *
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001117 * For DMA_TO_DEVICE this is out_iter, which is already pointing
1118 * to the right place.
1119 *
1120 * For DMA_FROM_DEVICE, the iovec will be just past the end
1121 * of the virtio-scsi response header in either the same
1122 * or immediately following iovec.
1123 *
1124 * Any associated T10_PI bytes for the outgoing / incoming
1125 * payloads are included in calculation of exp_data_len here.
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001126 */
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001127 prot_bytes = 0;
1128
1129 if (out_size > req_size) {
1130 data_direction = DMA_TO_DEVICE;
1131 exp_data_len = out_size - req_size;
1132 data_iter = out_iter;
1133 } else if (in_size > rsp_size) {
1134 data_direction = DMA_FROM_DEVICE;
1135 exp_data_len = in_size - rsp_size;
1136
1137 iov_iter_init(&in_iter, READ, &vq->iov[out], in,
1138 rsp_size + exp_data_len);
1139 iov_iter_advance(&in_iter, rsp_size);
1140 data_iter = in_iter;
1141 } else {
1142 data_direction = DMA_NONE;
1143 exp_data_len = 0;
1144 }
1145 /*
1146 * If T10_PI header + payload is present, setup prot_iter values
1147 * and recalculate data_iter for vhost_scsi_mapal() mapping to
1148 * host scatterlists via get_user_pages_fast().
1149 */
1150 if (t10_pi) {
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001151 if (v_req_pi.pi_bytesout) {
1152 if (data_direction != DMA_TO_DEVICE) {
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001153 vq_err(vq, "Received non zero pi_bytesout,"
1154 " but wrong data_direction\n");
Nicholas Bellingerde1419e2015-01-29 17:21:13 -08001155 vhost_scsi_send_bad_target(vs, vq, head, out);
1156 continue;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001157 }
Michael S. Tsirkine72fd722014-11-23 18:01:34 +02001158 prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesout);
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001159 } else if (v_req_pi.pi_bytesin) {
1160 if (data_direction != DMA_FROM_DEVICE) {
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001161 vq_err(vq, "Received non zero pi_bytesin,"
1162 " but wrong data_direction\n");
Nicholas Bellingerde1419e2015-01-29 17:21:13 -08001163 vhost_scsi_send_bad_target(vs, vq, head, out);
1164 continue;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001165 }
Michael S. Tsirkine72fd722014-11-23 18:01:34 +02001166 prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesin);
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001167 }
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001168 /*
1169 * Set prot_iter to data_iter, and advance past any
1170 * preceeding prot_bytes that may be present.
1171 *
1172 * Also fix up the exp_data_len to reflect only the
1173 * actual data payload length.
1174 */
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001175 if (prot_bytes) {
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001176 exp_data_len -= prot_bytes;
1177 prot_iter = data_iter;
1178 iov_iter_advance(&data_iter, prot_bytes);
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001179 }
Michael S. Tsirkine72fd722014-11-23 18:01:34 +02001180 tag = vhost64_to_cpu(vq, v_req_pi.tag);
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001181 task_attr = v_req_pi.task_attr;
1182 cdb = &v_req_pi.cdb[0];
1183 lun = ((v_req_pi.lun[2] << 8) | v_req_pi.lun[3]) & 0x3FFF;
1184 } else {
Michael S. Tsirkine72fd722014-11-23 18:01:34 +02001185 tag = vhost64_to_cpu(vq, v_req.tag);
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001186 task_attr = v_req.task_attr;
1187 cdb = &v_req.cdb[0];
1188 lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
1189 }
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001190 /*
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001191 * Check that the received CDB size does not exceeded our
1192 * hardcoded max for vhost-scsi, then get a pre-allocated
1193 * cmd descriptor for the new virtio-scsi tag.
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001194 *
1195 * TODO what if cdb was too small for varlen cdb header?
1196 */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001197 if (unlikely(scsi_command_size(cdb) > VHOST_SCSI_MAX_CDB_SIZE)) {
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001198 vq_err(vq, "Received SCSI CDB with command_size: %d that"
1199 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001200 scsi_command_size(cdb), VHOST_SCSI_MAX_CDB_SIZE);
Nicholas Bellingerde1419e2015-01-29 17:21:13 -08001201 vhost_scsi_send_bad_target(vs, vq, head, out);
1202 continue;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001203 }
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001204 cmd = vhost_scsi_get_tag(vq, tpg, cdb, tag, lun, task_attr,
Nicholas Bellinger9f977ef2014-06-10 01:19:38 -07001205 exp_data_len + prot_bytes,
1206 data_direction);
Asias He3c63f662013-05-06 16:38:29 +08001207 if (IS_ERR(cmd)) {
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -07001208 vq_err(vq, "vhost_scsi_get_tag failed %ld\n",
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001209 PTR_ERR(cmd));
Nicholas Bellingerde1419e2015-01-29 17:21:13 -08001210 vhost_scsi_send_bad_target(vs, vq, head, out);
1211 continue;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001212 }
Asias He3c63f662013-05-06 16:38:29 +08001213 cmd->tvc_vhost = vs;
1214 cmd->tvc_vq = vq;
Nicholas Bellinger79c14142015-01-27 13:13:12 -08001215 cmd->tvc_resp_iov = &vq->iov[out];
1216 cmd->tvc_in_iovs = in;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001217
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001218 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001219 cmd->tvc_cdb[0], cmd->tvc_lun);
1220 pr_debug("cmd: %p exp_data_len: %d, prot_bytes: %d data_direction:"
1221 " %d\n", cmd, exp_data_len, prot_bytes, data_direction);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001222
1223 if (data_direction != DMA_NONE) {
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001224 ret = vhost_scsi_mapal(cmd,
1225 prot_bytes, &prot_iter,
1226 exp_data_len, &data_iter);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001227 if (unlikely(ret)) {
1228 vq_err(vq, "Failed to map iov to sgl\n");
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001229 vhost_scsi_release_cmd(&cmd->tvc_se_cmd);
Nicholas Bellingerde1419e2015-01-29 17:21:13 -08001230 vhost_scsi_send_bad_target(vs, vq, head, out);
1231 continue;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001232 }
1233 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001234 /*
1235 * Save the descriptor from vhost_get_vq_desc() to be used to
1236 * complete the virtio-scsi request in TCM callback context via
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001237 * vhost_scsi_queue_data_in() and vhost_scsi_queue_status()
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001238 */
Asias He3c63f662013-05-06 16:38:29 +08001239 cmd->tvc_vq_desc = head;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001240 /*
Nicholas Bellinger09b13fa2015-01-25 21:14:58 -08001241 * Dispatch cmd descriptor for cmwq execution in process
1242 * context provided by vhost_scsi_workqueue. This also ensures
1243 * cmd is executed on the same kworker CPU as this vhost
1244 * thread to gain positive L2 cache locality effects.
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001245 */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001246 INIT_WORK(&cmd->work, vhost_scsi_submission_work);
1247 queue_work(vhost_scsi_workqueue, &cmd->work);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001248 }
Asias Hee7802212013-05-07 14:54:35 +08001249out:
Asias He7ea206c2013-04-10 15:06:14 +08001250 mutex_unlock(&vq->mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001251}
1252
1253static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
1254{
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001255 pr_debug("%s: The handling func for control queue.\n", __func__);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001256}
1257
Asias He683bd962013-05-06 16:38:27 +08001258static void
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001259vhost_scsi_send_evt(struct vhost_scsi *vs,
1260 struct vhost_scsi_tpg *tpg,
Asias He683bd962013-05-06 16:38:27 +08001261 struct se_lun *lun,
1262 u32 event,
1263 u32 reason)
Asias Hea6c9af82013-04-25 15:35:21 +08001264{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001265 struct vhost_scsi_evt *evt;
Asias Hea6c9af82013-04-25 15:35:21 +08001266
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001267 evt = vhost_scsi_allocate_evt(vs, event, reason);
Asias Hea6c9af82013-04-25 15:35:21 +08001268 if (!evt)
1269 return;
1270
1271 if (tpg && lun) {
1272 /* TODO: share lun setup code with virtio-scsi.ko */
1273 /*
1274 * Note: evt->event is zeroed when we allocate it and
1275 * lun[4-7] need to be zero according to virtio-scsi spec.
1276 */
1277 evt->event.lun[0] = 0x01;
Dan Carpenter59c816c2015-02-05 10:37:33 +03001278 evt->event.lun[1] = tpg->tport_tpgt;
Asias Hea6c9af82013-04-25 15:35:21 +08001279 if (lun->unpacked_lun >= 256)
1280 evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
1281 evt->event.lun[3] = lun->unpacked_lun & 0xFF;
1282 }
1283
1284 llist_add(&evt->list, &vs->vs_event_list);
1285 vhost_work_queue(&vs->dev, &vs->vs_event_work);
1286}
1287
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001288static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
1289{
Asias Hea6c9af82013-04-25 15:35:21 +08001290 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1291 poll.work);
1292 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1293
1294 mutex_lock(&vq->mutex);
1295 if (!vq->private_data)
1296 goto out;
1297
1298 if (vs->vs_events_missed)
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001299 vhost_scsi_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
Asias Hea6c9af82013-04-25 15:35:21 +08001300out:
1301 mutex_unlock(&vq->mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001302}
1303
1304static void vhost_scsi_handle_kick(struct vhost_work *work)
1305{
1306 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1307 poll.work);
1308 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1309
Asias He1b7f3902013-02-06 13:20:59 +08001310 vhost_scsi_handle_vq(vs, vq);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001311}
1312
Asias He4f7f46d2013-04-03 14:17:37 +08001313static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
1314{
Asias He3ab2e422013-04-27 11:16:48 +08001315 vhost_poll_flush(&vs->vqs[index].vq.poll);
Asias He4f7f46d2013-04-03 14:17:37 +08001316}
1317
Michael S. Tsirkin3dfbff32013-04-28 15:38:52 +03001318/* Callers must hold dev mutex */
Asias He4f7f46d2013-04-03 14:17:37 +08001319static void vhost_scsi_flush(struct vhost_scsi *vs)
1320{
Asias Hef2f0173d2013-04-27 11:16:49 +08001321 struct vhost_scsi_inflight *old_inflight[VHOST_SCSI_MAX_VQ];
Asias He4f7f46d2013-04-03 14:17:37 +08001322 int i;
1323
Asias Hef2f0173d2013-04-27 11:16:49 +08001324 /* Init new inflight and remember the old inflight */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001325 vhost_scsi_init_inflight(vs, old_inflight);
Asias Hef2f0173d2013-04-27 11:16:49 +08001326
1327 /*
1328 * The inflight->kref was initialized to 1. We decrement it here to
1329 * indicate the start of the flush operation so that it will reach 0
1330 * when all the reqs are finished.
1331 */
1332 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001333 kref_put(&old_inflight[i]->kref, vhost_scsi_done_inflight);
Asias Hef2f0173d2013-04-27 11:16:49 +08001334
1335 /* Flush both the vhost poll and vhost work */
Asias He4f7f46d2013-04-03 14:17:37 +08001336 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1337 vhost_scsi_flush_vq(vs, i);
1338 vhost_work_flush(&vs->dev, &vs->vs_completion_work);
Asias Hea6c9af82013-04-25 15:35:21 +08001339 vhost_work_flush(&vs->dev, &vs->vs_event_work);
Asias Hef2f0173d2013-04-27 11:16:49 +08001340
1341 /* Wait for all reqs issued before the flush to be finished */
1342 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1343 wait_for_completion(&old_inflight[i]->comp);
Asias He4f7f46d2013-04-03 14:17:37 +08001344}
1345
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001346/*
1347 * Called from vhost_scsi_ioctl() context to walk the list of available
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001348 * vhost_scsi_tpg with an active struct vhost_scsi_nexus
Asias Hef2b7daf2013-04-25 15:35:20 +08001349 *
1350 * The lock nesting rule is:
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001351 * vhost_scsi_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001352 */
Asias He683bd962013-05-06 16:38:27 +08001353static int
1354vhost_scsi_set_endpoint(struct vhost_scsi *vs,
1355 struct vhost_scsi_target *t)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001356{
Nicholas Bellingerab8edab2014-10-08 06:19:20 +00001357 struct se_portal_group *se_tpg;
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001358 struct vhost_scsi_tport *tv_tport;
1359 struct vhost_scsi_tpg *tpg;
1360 struct vhost_scsi_tpg **vs_tpg;
Asias He4f7f46d2013-04-03 14:17:37 +08001361 struct vhost_virtqueue *vq;
1362 int index, ret, i, len;
Asias He67e18cf2013-02-05 12:31:57 +08001363 bool match = false;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001364
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001365 mutex_lock(&vhost_scsi_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001366 mutex_lock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001367
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001368 /* Verify that ring has been setup correctly. */
1369 for (index = 0; index < vs->dev.nvqs; ++index) {
1370 /* Verify that ring has been setup correctly. */
Asias He3ab2e422013-04-27 11:16:48 +08001371 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
Asias Hef2b7daf2013-04-25 15:35:20 +08001372 ret = -EFAULT;
1373 goto out;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001374 }
1375 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001376
Asias He4f7f46d2013-04-03 14:17:37 +08001377 len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
1378 vs_tpg = kzalloc(len, GFP_KERNEL);
1379 if (!vs_tpg) {
Asias Hef2b7daf2013-04-25 15:35:20 +08001380 ret = -ENOMEM;
1381 goto out;
Asias He4f7f46d2013-04-03 14:17:37 +08001382 }
1383 if (vs->vs_tpg)
1384 memcpy(vs_tpg, vs->vs_tpg, len);
1385
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001386 list_for_each_entry(tpg, &vhost_scsi_list, tv_tpg_list) {
Asias He98718312013-05-06 16:38:28 +08001387 mutex_lock(&tpg->tv_tpg_mutex);
1388 if (!tpg->tpg_nexus) {
1389 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001390 continue;
1391 }
Asias He98718312013-05-06 16:38:28 +08001392 if (tpg->tv_tpg_vhost_count != 0) {
1393 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001394 continue;
1395 }
Asias He98718312013-05-06 16:38:28 +08001396 tv_tport = tpg->tport;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001397
Asias He67e18cf2013-02-05 12:31:57 +08001398 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
Asias He98718312013-05-06 16:38:28 +08001399 if (vs->vs_tpg && vs->vs_tpg[tpg->tport_tpgt]) {
Asias He4f7f46d2013-04-03 14:17:37 +08001400 kfree(vs_tpg);
Asias He98718312013-05-06 16:38:28 +08001401 mutex_unlock(&tpg->tv_tpg_mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001402 ret = -EEXIST;
1403 goto out;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001404 }
Nicholas Bellingerab8edab2014-10-08 06:19:20 +00001405 /*
1406 * In order to ensure individual vhost-scsi configfs
1407 * groups cannot be removed while in use by vhost ioctl,
1408 * go ahead and take an explicit se_tpg->tpg_group.cg_item
1409 * dependency now.
1410 */
1411 se_tpg = &tpg->se_tpg;
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001412 ret = target_depend_item(&se_tpg->tpg_group.cg_item);
Nicholas Bellingerab8edab2014-10-08 06:19:20 +00001413 if (ret) {
1414 pr_warn("configfs_depend_item() failed: %d\n", ret);
1415 kfree(vs_tpg);
1416 mutex_unlock(&tpg->tv_tpg_mutex);
1417 goto out;
1418 }
Asias He98718312013-05-06 16:38:28 +08001419 tpg->tv_tpg_vhost_count++;
1420 tpg->vhost_scsi = vs;
1421 vs_tpg[tpg->tport_tpgt] = tpg;
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001422 smp_mb__after_atomic();
Asias He67e18cf2013-02-05 12:31:57 +08001423 match = true;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001424 }
Asias He98718312013-05-06 16:38:28 +08001425 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001426 }
Asias He67e18cf2013-02-05 12:31:57 +08001427
1428 if (match) {
1429 memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
1430 sizeof(vs->vs_vhost_wwpn));
Asias He4f7f46d2013-04-03 14:17:37 +08001431 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
Asias He3ab2e422013-04-27 11:16:48 +08001432 vq = &vs->vqs[i].vq;
Asias He4f7f46d2013-04-03 14:17:37 +08001433 mutex_lock(&vq->mutex);
Asias He22fa90c2013-05-07 14:54:36 +08001434 vq->private_data = vs_tpg;
Asias Hedfd5d562013-04-03 14:17:38 +08001435 vhost_init_used(vq);
Asias He4f7f46d2013-04-03 14:17:37 +08001436 mutex_unlock(&vq->mutex);
1437 }
Asias He67e18cf2013-02-05 12:31:57 +08001438 ret = 0;
1439 } else {
1440 ret = -EEXIST;
1441 }
1442
Asias He4f7f46d2013-04-03 14:17:37 +08001443 /*
1444 * Act as synchronize_rcu to make sure access to
1445 * old vs->vs_tpg is finished.
1446 */
1447 vhost_scsi_flush(vs);
1448 kfree(vs->vs_tpg);
1449 vs->vs_tpg = vs_tpg;
1450
Asias Hef2b7daf2013-04-25 15:35:20 +08001451out:
Asias He67e18cf2013-02-05 12:31:57 +08001452 mutex_unlock(&vs->dev.mutex);
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001453 mutex_unlock(&vhost_scsi_mutex);
Asias He67e18cf2013-02-05 12:31:57 +08001454 return ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001455}
1456
Asias He683bd962013-05-06 16:38:27 +08001457static int
1458vhost_scsi_clear_endpoint(struct vhost_scsi *vs,
1459 struct vhost_scsi_target *t)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001460{
Nicholas Bellingerab8edab2014-10-08 06:19:20 +00001461 struct se_portal_group *se_tpg;
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001462 struct vhost_scsi_tport *tv_tport;
1463 struct vhost_scsi_tpg *tpg;
Asias He4f7f46d2013-04-03 14:17:37 +08001464 struct vhost_virtqueue *vq;
1465 bool match = false;
Asias He67e18cf2013-02-05 12:31:57 +08001466 int index, ret, i;
1467 u8 target;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001468
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001469 mutex_lock(&vhost_scsi_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001470 mutex_lock(&vs->dev.mutex);
1471 /* Verify that ring has been setup correctly. */
1472 for (index = 0; index < vs->dev.nvqs; ++index) {
Asias He3ab2e422013-04-27 11:16:48 +08001473 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001474 ret = -EFAULT;
Asias He038e0af2013-03-15 09:14:05 +08001475 goto err_dev;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001476 }
1477 }
Asias He4f7f46d2013-04-03 14:17:37 +08001478
1479 if (!vs->vs_tpg) {
Asias Hef2b7daf2013-04-25 15:35:20 +08001480 ret = 0;
1481 goto err_dev;
Asias He4f7f46d2013-04-03 14:17:37 +08001482 }
1483
Asias He67e18cf2013-02-05 12:31:57 +08001484 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
1485 target = i;
Asias He98718312013-05-06 16:38:28 +08001486 tpg = vs->vs_tpg[target];
1487 if (!tpg)
Asias He67e18cf2013-02-05 12:31:57 +08001488 continue;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001489
Asias He98718312013-05-06 16:38:28 +08001490 mutex_lock(&tpg->tv_tpg_mutex);
1491 tv_tport = tpg->tport;
Asias He67e18cf2013-02-05 12:31:57 +08001492 if (!tv_tport) {
1493 ret = -ENODEV;
Asias He038e0af2013-03-15 09:14:05 +08001494 goto err_tpg;
Asias He67e18cf2013-02-05 12:31:57 +08001495 }
1496
1497 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
Asias He98718312013-05-06 16:38:28 +08001498 pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu"
Asias He67e18cf2013-02-05 12:31:57 +08001499 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
Asias He98718312013-05-06 16:38:28 +08001500 tv_tport->tport_name, tpg->tport_tpgt,
Asias He67e18cf2013-02-05 12:31:57 +08001501 t->vhost_wwpn, t->vhost_tpgt);
1502 ret = -EINVAL;
Asias He038e0af2013-03-15 09:14:05 +08001503 goto err_tpg;
Asias He67e18cf2013-02-05 12:31:57 +08001504 }
Asias He98718312013-05-06 16:38:28 +08001505 tpg->tv_tpg_vhost_count--;
1506 tpg->vhost_scsi = NULL;
Asias He67e18cf2013-02-05 12:31:57 +08001507 vs->vs_tpg[target] = NULL;
Asias He4f7f46d2013-04-03 14:17:37 +08001508 match = true;
Asias He98718312013-05-06 16:38:28 +08001509 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellingerab8edab2014-10-08 06:19:20 +00001510 /*
1511 * Release se_tpg->tpg_group.cg_item configfs dependency now
1512 * to allow vhost-scsi WWPN se_tpg->tpg_group shutdown to occur.
1513 */
1514 se_tpg = &tpg->se_tpg;
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001515 target_undepend_item(&se_tpg->tpg_group.cg_item);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001516 }
Asias He4f7f46d2013-04-03 14:17:37 +08001517 if (match) {
1518 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
Asias He3ab2e422013-04-27 11:16:48 +08001519 vq = &vs->vqs[i].vq;
Asias He4f7f46d2013-04-03 14:17:37 +08001520 mutex_lock(&vq->mutex);
Asias He22fa90c2013-05-07 14:54:36 +08001521 vq->private_data = NULL;
Asias He4f7f46d2013-04-03 14:17:37 +08001522 mutex_unlock(&vq->mutex);
1523 }
1524 }
1525 /*
1526 * Act as synchronize_rcu to make sure access to
1527 * old vs->vs_tpg is finished.
1528 */
1529 vhost_scsi_flush(vs);
1530 kfree(vs->vs_tpg);
1531 vs->vs_tpg = NULL;
Asias Hea6c9af82013-04-25 15:35:21 +08001532 WARN_ON(vs->vs_events_nr);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001533 mutex_unlock(&vs->dev.mutex);
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001534 mutex_unlock(&vhost_scsi_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001535 return 0;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001536
Asias He038e0af2013-03-15 09:14:05 +08001537err_tpg:
Asias He98718312013-05-06 16:38:28 +08001538 mutex_unlock(&tpg->tv_tpg_mutex);
Asias He038e0af2013-03-15 09:14:05 +08001539err_dev:
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001540 mutex_unlock(&vs->dev.mutex);
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001541 mutex_unlock(&vhost_scsi_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001542 return ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001543}
1544
Asias He4f7f46d2013-04-03 14:17:37 +08001545static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
1546{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001547 struct vhost_virtqueue *vq;
1548 int i;
1549
Asias He4f7f46d2013-04-03 14:17:37 +08001550 if (features & ~VHOST_SCSI_FEATURES)
1551 return -EOPNOTSUPP;
1552
1553 mutex_lock(&vs->dev.mutex);
1554 if ((features & (1 << VHOST_F_LOG_ALL)) &&
1555 !vhost_log_access_ok(&vs->dev)) {
1556 mutex_unlock(&vs->dev.mutex);
1557 return -EFAULT;
1558 }
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001559
1560 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1561 vq = &vs->vqs[i].vq;
1562 mutex_lock(&vq->mutex);
1563 vq->acked_features = features;
1564 mutex_unlock(&vq->mutex);
1565 }
Asias He4f7f46d2013-04-03 14:17:37 +08001566 mutex_unlock(&vs->dev.mutex);
1567 return 0;
1568}
1569
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001570static int vhost_scsi_open(struct inode *inode, struct file *f)
1571{
Asias Hec7289312013-05-06 16:38:26 +08001572 struct vhost_scsi *vs;
Asias He3ab2e422013-04-27 11:16:48 +08001573 struct vhost_virtqueue **vqs;
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001574 int r = -ENOMEM, i;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001575
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001576 vs = kzalloc(sizeof(*vs), GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
1577 if (!vs) {
1578 vs = vzalloc(sizeof(*vs));
1579 if (!vs)
1580 goto err_vs;
1581 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001582
Asias He3ab2e422013-04-27 11:16:48 +08001583 vqs = kmalloc(VHOST_SCSI_MAX_VQ * sizeof(*vqs), GFP_KERNEL);
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001584 if (!vqs)
1585 goto err_vqs;
Asias He3ab2e422013-04-27 11:16:48 +08001586
Asias Hec7289312013-05-06 16:38:26 +08001587 vhost_work_init(&vs->vs_completion_work, vhost_scsi_complete_cmd_work);
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001588 vhost_work_init(&vs->vs_event_work, vhost_scsi_evt_work);
Asias Hea6c9af82013-04-25 15:35:21 +08001589
Asias Hec7289312013-05-06 16:38:26 +08001590 vs->vs_events_nr = 0;
1591 vs->vs_events_missed = false;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001592
Asias Hec7289312013-05-06 16:38:26 +08001593 vqs[VHOST_SCSI_VQ_CTL] = &vs->vqs[VHOST_SCSI_VQ_CTL].vq;
1594 vqs[VHOST_SCSI_VQ_EVT] = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1595 vs->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick;
1596 vs->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick;
Asias He3ab2e422013-04-27 11:16:48 +08001597 for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) {
Asias Hec7289312013-05-06 16:38:26 +08001598 vqs[i] = &vs->vqs[i].vq;
1599 vs->vqs[i].vq.handle_kick = vhost_scsi_handle_kick;
Asias He3ab2e422013-04-27 11:16:48 +08001600 }
Zhi Yong Wu59566b6e2013-12-07 04:13:03 +08001601 vhost_dev_init(&vs->dev, vqs, VHOST_SCSI_MAX_VQ);
Asias Hef2f0173d2013-04-27 11:16:49 +08001602
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001603 vhost_scsi_init_inflight(vs, NULL);
Asias Hef2f0173d2013-04-27 11:16:49 +08001604
Asias Hec7289312013-05-06 16:38:26 +08001605 f->private_data = vs;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001606 return 0;
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001607
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001608err_vqs:
Michael S. Tsirkin684044412014-06-12 19:00:01 +03001609 kvfree(vs);
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001610err_vs:
1611 return r;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001612}
1613
1614static int vhost_scsi_release(struct inode *inode, struct file *f)
1615{
Asias Hec7289312013-05-06 16:38:26 +08001616 struct vhost_scsi *vs = f->private_data;
Asias He67e18cf2013-02-05 12:31:57 +08001617 struct vhost_scsi_target t;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001618
Asias Hec7289312013-05-06 16:38:26 +08001619 mutex_lock(&vs->dev.mutex);
1620 memcpy(t.vhost_wwpn, vs->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
1621 mutex_unlock(&vs->dev.mutex);
1622 vhost_scsi_clear_endpoint(vs, &t);
1623 vhost_dev_stop(&vs->dev);
1624 vhost_dev_cleanup(&vs->dev, false);
Asias Hea6c9af82013-04-25 15:35:21 +08001625 /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
Asias Hec7289312013-05-06 16:38:26 +08001626 vhost_scsi_flush(vs);
1627 kfree(vs->dev.vqs);
Michael S. Tsirkin684044412014-06-12 19:00:01 +03001628 kvfree(vs);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001629 return 0;
1630}
1631
Asias He683bd962013-05-06 16:38:27 +08001632static long
1633vhost_scsi_ioctl(struct file *f,
1634 unsigned int ioctl,
1635 unsigned long arg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001636{
1637 struct vhost_scsi *vs = f->private_data;
1638 struct vhost_scsi_target backend;
1639 void __user *argp = (void __user *)arg;
1640 u64 __user *featurep = argp;
Asias He11c634182013-04-25 15:35:22 +08001641 u32 __user *eventsp = argp;
1642 u32 events_missed;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001643 u64 features;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001644 int r, abi_version = VHOST_SCSI_ABI_VERSION;
Asias He3ab2e422013-04-27 11:16:48 +08001645 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001646
1647 switch (ioctl) {
1648 case VHOST_SCSI_SET_ENDPOINT:
1649 if (copy_from_user(&backend, argp, sizeof backend))
1650 return -EFAULT;
Michael S. Tsirkin6de71452012-08-18 15:44:09 -07001651 if (backend.reserved != 0)
1652 return -EOPNOTSUPP;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001653
1654 return vhost_scsi_set_endpoint(vs, &backend);
1655 case VHOST_SCSI_CLEAR_ENDPOINT:
1656 if (copy_from_user(&backend, argp, sizeof backend))
1657 return -EFAULT;
Michael S. Tsirkin6de71452012-08-18 15:44:09 -07001658 if (backend.reserved != 0)
1659 return -EOPNOTSUPP;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001660
1661 return vhost_scsi_clear_endpoint(vs, &backend);
1662 case VHOST_SCSI_GET_ABI_VERSION:
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001663 if (copy_to_user(argp, &abi_version, sizeof abi_version))
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001664 return -EFAULT;
1665 return 0;
Asias He11c634182013-04-25 15:35:22 +08001666 case VHOST_SCSI_SET_EVENTS_MISSED:
1667 if (get_user(events_missed, eventsp))
1668 return -EFAULT;
1669 mutex_lock(&vq->mutex);
1670 vs->vs_events_missed = events_missed;
1671 mutex_unlock(&vq->mutex);
1672 return 0;
1673 case VHOST_SCSI_GET_EVENTS_MISSED:
1674 mutex_lock(&vq->mutex);
1675 events_missed = vs->vs_events_missed;
1676 mutex_unlock(&vq->mutex);
1677 if (put_user(events_missed, eventsp))
1678 return -EFAULT;
1679 return 0;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001680 case VHOST_GET_FEATURES:
Nicholas Bellinger5dade712013-03-27 17:23:41 -07001681 features = VHOST_SCSI_FEATURES;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001682 if (copy_to_user(featurep, &features, sizeof features))
1683 return -EFAULT;
1684 return 0;
1685 case VHOST_SET_FEATURES:
1686 if (copy_from_user(&features, featurep, sizeof features))
1687 return -EFAULT;
1688 return vhost_scsi_set_features(vs, features);
1689 default:
1690 mutex_lock(&vs->dev.mutex);
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001691 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
1692 /* TODO: flush backend after dev ioctl. */
1693 if (r == -ENOIOCTLCMD)
1694 r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001695 mutex_unlock(&vs->dev.mutex);
1696 return r;
1697 }
1698}
1699
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001700#ifdef CONFIG_COMPAT
1701static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
1702 unsigned long arg)
1703{
1704 return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1705}
1706#endif
1707
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001708static const struct file_operations vhost_scsi_fops = {
1709 .owner = THIS_MODULE,
1710 .release = vhost_scsi_release,
1711 .unlocked_ioctl = vhost_scsi_ioctl,
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001712#ifdef CONFIG_COMPAT
1713 .compat_ioctl = vhost_scsi_compat_ioctl,
1714#endif
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001715 .open = vhost_scsi_open,
1716 .llseek = noop_llseek,
1717};
1718
1719static struct miscdevice vhost_scsi_misc = {
1720 MISC_DYNAMIC_MINOR,
1721 "vhost-scsi",
1722 &vhost_scsi_fops,
1723};
1724
1725static int __init vhost_scsi_register(void)
1726{
1727 return misc_register(&vhost_scsi_misc);
1728}
1729
1730static int vhost_scsi_deregister(void)
1731{
1732 return misc_deregister(&vhost_scsi_misc);
1733}
1734
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001735static char *vhost_scsi_dump_proto_id(struct vhost_scsi_tport *tport)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001736{
1737 switch (tport->tport_proto_id) {
1738 case SCSI_PROTOCOL_SAS:
1739 return "SAS";
1740 case SCSI_PROTOCOL_FCP:
1741 return "FCP";
1742 case SCSI_PROTOCOL_ISCSI:
1743 return "iSCSI";
1744 default:
1745 break;
1746 }
1747
1748 return "Unknown";
1749}
1750
Asias He683bd962013-05-06 16:38:27 +08001751static void
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001752vhost_scsi_do_plug(struct vhost_scsi_tpg *tpg,
Asias He683bd962013-05-06 16:38:27 +08001753 struct se_lun *lun, bool plug)
Asias Hea6c9af82013-04-25 15:35:21 +08001754{
1755
1756 struct vhost_scsi *vs = tpg->vhost_scsi;
1757 struct vhost_virtqueue *vq;
1758 u32 reason;
1759
1760 if (!vs)
1761 return;
1762
1763 mutex_lock(&vs->dev.mutex);
Asias Hea6c9af82013-04-25 15:35:21 +08001764
1765 if (plug)
1766 reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
1767 else
1768 reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
1769
Asias He3ab2e422013-04-27 11:16:48 +08001770 vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Asias Hea6c9af82013-04-25 15:35:21 +08001771 mutex_lock(&vq->mutex);
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001772 if (vhost_has_feature(vq, VIRTIO_SCSI_F_HOTPLUG))
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001773 vhost_scsi_send_evt(vs, tpg, lun,
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001774 VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
Asias Hea6c9af82013-04-25 15:35:21 +08001775 mutex_unlock(&vq->mutex);
1776 mutex_unlock(&vs->dev.mutex);
1777}
1778
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001779static void vhost_scsi_hotplug(struct vhost_scsi_tpg *tpg, struct se_lun *lun)
Asias Hea6c9af82013-04-25 15:35:21 +08001780{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001781 vhost_scsi_do_plug(tpg, lun, true);
Asias Hea6c9af82013-04-25 15:35:21 +08001782}
1783
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001784static void vhost_scsi_hotunplug(struct vhost_scsi_tpg *tpg, struct se_lun *lun)
Asias Hea6c9af82013-04-25 15:35:21 +08001785{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001786 vhost_scsi_do_plug(tpg, lun, false);
Asias Hea6c9af82013-04-25 15:35:21 +08001787}
1788
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001789static int vhost_scsi_port_link(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08001790 struct se_lun *lun)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001791{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001792 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
1793 struct vhost_scsi_tpg, se_tpg);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001794
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001795 mutex_lock(&vhost_scsi_mutex);
Asias Hea6c9af82013-04-25 15:35:21 +08001796
Asias He98718312013-05-06 16:38:28 +08001797 mutex_lock(&tpg->tv_tpg_mutex);
1798 tpg->tv_tpg_port_count++;
1799 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001800
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001801 vhost_scsi_hotplug(tpg, lun);
Asias Hea6c9af82013-04-25 15:35:21 +08001802
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001803 mutex_unlock(&vhost_scsi_mutex);
Asias Hea6c9af82013-04-25 15:35:21 +08001804
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001805 return 0;
1806}
1807
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001808static void vhost_scsi_port_unlink(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08001809 struct se_lun *lun)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001810{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001811 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
1812 struct vhost_scsi_tpg, se_tpg);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001813
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001814 mutex_lock(&vhost_scsi_mutex);
Asias Hea6c9af82013-04-25 15:35:21 +08001815
Asias He98718312013-05-06 16:38:28 +08001816 mutex_lock(&tpg->tv_tpg_mutex);
1817 tpg->tv_tpg_port_count--;
1818 mutex_unlock(&tpg->tv_tpg_mutex);
Asias Hea6c9af82013-04-25 15:35:21 +08001819
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001820 vhost_scsi_hotunplug(tpg, lun);
Asias Hea6c9af82013-04-25 15:35:21 +08001821
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001822 mutex_unlock(&vhost_scsi_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001823}
1824
Asias He683bd962013-05-06 16:38:27 +08001825static struct se_node_acl *
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001826vhost_scsi_make_nodeacl(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08001827 struct config_group *group,
1828 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001829{
1830 struct se_node_acl *se_nacl, *se_nacl_new;
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001831 struct vhost_scsi_nacl *nacl;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001832 u64 wwpn = 0;
1833 u32 nexus_depth;
1834
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001835 /* vhost_scsi_parse_wwn(name, &wwpn, 1) < 0)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001836 return ERR_PTR(-EINVAL); */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001837 se_nacl_new = vhost_scsi_alloc_fabric_acl(se_tpg);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001838 if (!se_nacl_new)
1839 return ERR_PTR(-ENOMEM);
1840
1841 nexus_depth = 1;
1842 /*
1843 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1844 * when converting a NodeACL from demo mode -> explict
1845 */
1846 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1847 name, nexus_depth);
1848 if (IS_ERR(se_nacl)) {
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001849 vhost_scsi_release_fabric_acl(se_tpg, se_nacl_new);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001850 return se_nacl;
1851 }
1852 /*
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001853 * Locate our struct vhost_scsi_nacl and set the FC Nport WWPN
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001854 */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001855 nacl = container_of(se_nacl, struct vhost_scsi_nacl, se_node_acl);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001856 nacl->iport_wwpn = wwpn;
1857
1858 return se_nacl;
1859}
1860
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001861static void vhost_scsi_drop_nodeacl(struct se_node_acl *se_acl)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001862{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001863 struct vhost_scsi_nacl *nacl = container_of(se_acl,
1864 struct vhost_scsi_nacl, se_node_acl);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001865 core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1866 kfree(nacl);
1867}
1868
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001869static void vhost_scsi_free_cmd_map_res(struct vhost_scsi_nexus *nexus,
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001870 struct se_session *se_sess)
1871{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001872 struct vhost_scsi_cmd *tv_cmd;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001873 unsigned int i;
1874
1875 if (!se_sess->sess_cmd_map)
1876 return;
1877
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001878 for (i = 0; i < VHOST_SCSI_DEFAULT_TAGS; i++) {
1879 tv_cmd = &((struct vhost_scsi_cmd *)se_sess->sess_cmd_map)[i];
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001880
1881 kfree(tv_cmd->tvc_sgl);
Nicholas Bellingerb1935f62014-02-22 18:08:24 -08001882 kfree(tv_cmd->tvc_prot_sgl);
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001883 kfree(tv_cmd->tvc_upages);
1884 }
1885}
1886
Nicholas Bellingerb1d75fe2015-03-28 00:03:51 -07001887static ssize_t vhost_scsi_tpg_attrib_store_fabric_prot_type(
1888 struct se_portal_group *se_tpg,
1889 const char *page,
1890 size_t count)
1891{
1892 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
1893 struct vhost_scsi_tpg, se_tpg);
1894 unsigned long val;
1895 int ret = kstrtoul(page, 0, &val);
1896
1897 if (ret) {
1898 pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
1899 return ret;
1900 }
1901 if (val != 0 && val != 1 && val != 3) {
1902 pr_err("Invalid vhost_scsi fabric_prot_type: %lu\n", val);
1903 return -EINVAL;
1904 }
1905 tpg->tv_fabric_prot_type = val;
1906
1907 return count;
1908}
1909
1910static ssize_t vhost_scsi_tpg_attrib_show_fabric_prot_type(
1911 struct se_portal_group *se_tpg,
1912 char *page)
1913{
1914 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
1915 struct vhost_scsi_tpg, se_tpg);
1916
1917 return sprintf(page, "%d\n", tpg->tv_fabric_prot_type);
1918}
1919TF_TPG_ATTRIB_ATTR(vhost_scsi, fabric_prot_type, S_IRUGO | S_IWUSR);
1920
1921static struct configfs_attribute *vhost_scsi_tpg_attrib_attrs[] = {
1922 &vhost_scsi_tpg_attrib_fabric_prot_type.attr,
1923 NULL,
1924};
1925
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001926static int vhost_scsi_make_nexus(struct vhost_scsi_tpg *tpg,
Asias He683bd962013-05-06 16:38:27 +08001927 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001928{
1929 struct se_portal_group *se_tpg;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001930 struct se_session *se_sess;
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001931 struct vhost_scsi_nexus *tv_nexus;
1932 struct vhost_scsi_cmd *tv_cmd;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001933 unsigned int i;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001934
Asias He98718312013-05-06 16:38:28 +08001935 mutex_lock(&tpg->tv_tpg_mutex);
1936 if (tpg->tpg_nexus) {
1937 mutex_unlock(&tpg->tv_tpg_mutex);
1938 pr_debug("tpg->tpg_nexus already exists\n");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001939 return -EEXIST;
1940 }
Asias He98718312013-05-06 16:38:28 +08001941 se_tpg = &tpg->se_tpg;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001942
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001943 tv_nexus = kzalloc(sizeof(struct vhost_scsi_nexus), GFP_KERNEL);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001944 if (!tv_nexus) {
Asias He98718312013-05-06 16:38:28 +08001945 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001946 pr_err("Unable to allocate struct vhost_scsi_nexus\n");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001947 return -ENOMEM;
1948 }
1949 /*
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -07001950 * Initialize the struct se_session pointer and setup tagpool
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001951 * for struct vhost_scsi_cmd descriptors
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001952 */
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -07001953 tv_nexus->tvn_se_sess = transport_init_session_tags(
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001954 VHOST_SCSI_DEFAULT_TAGS,
1955 sizeof(struct vhost_scsi_cmd),
Nicholas Bellinger95e7c432014-02-22 18:22:31 -08001956 TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001957 if (IS_ERR(tv_nexus->tvn_se_sess)) {
Asias He98718312013-05-06 16:38:28 +08001958 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001959 kfree(tv_nexus);
1960 return -ENOMEM;
1961 }
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001962 se_sess = tv_nexus->tvn_se_sess;
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001963 for (i = 0; i < VHOST_SCSI_DEFAULT_TAGS; i++) {
1964 tv_cmd = &((struct vhost_scsi_cmd *)se_sess->sess_cmd_map)[i];
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001965
1966 tv_cmd->tvc_sgl = kzalloc(sizeof(struct scatterlist) *
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001967 VHOST_SCSI_PREALLOC_SGLS, GFP_KERNEL);
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001968 if (!tv_cmd->tvc_sgl) {
1969 mutex_unlock(&tpg->tv_tpg_mutex);
1970 pr_err("Unable to allocate tv_cmd->tvc_sgl\n");
1971 goto out;
1972 }
1973
1974 tv_cmd->tvc_upages = kzalloc(sizeof(struct page *) *
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001975 VHOST_SCSI_PREALLOC_UPAGES, GFP_KERNEL);
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001976 if (!tv_cmd->tvc_upages) {
1977 mutex_unlock(&tpg->tv_tpg_mutex);
1978 pr_err("Unable to allocate tv_cmd->tvc_upages\n");
1979 goto out;
1980 }
Nicholas Bellingerb1935f62014-02-22 18:08:24 -08001981
1982 tv_cmd->tvc_prot_sgl = kzalloc(sizeof(struct scatterlist) *
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001983 VHOST_SCSI_PREALLOC_PROT_SGLS, GFP_KERNEL);
Nicholas Bellingerb1935f62014-02-22 18:08:24 -08001984 if (!tv_cmd->tvc_prot_sgl) {
1985 mutex_unlock(&tpg->tv_tpg_mutex);
1986 pr_err("Unable to allocate tv_cmd->tvc_prot_sgl\n");
1987 goto out;
1988 }
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07001989 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001990 /*
1991 * Since we are running in 'demo mode' this call with generate a
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001992 * struct se_node_acl for the vhost_scsi struct se_portal_group with
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001993 * the SCSI Initiator port name of the passed configfs group 'name'.
1994 */
1995 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1996 se_tpg, (unsigned char *)name);
1997 if (!tv_nexus->tvn_se_sess->se_node_acl) {
Asias He98718312013-05-06 16:38:28 +08001998 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001999 pr_debug("core_tpg_check_initiator_node_acl() failed"
2000 " for %s\n", name);
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07002001 goto out;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002002 }
2003 /*
Bart Van Assche2f450cc2015-02-12 11:48:49 +01002004 * Now register the TCM vhost virtual I_T Nexus as active.
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002005 */
Bart Van Assche2f450cc2015-02-12 11:48:49 +01002006 transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002007 tv_nexus->tvn_se_sess, tv_nexus);
Asias He98718312013-05-06 16:38:28 +08002008 tpg->tpg_nexus = tv_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002009
Asias He98718312013-05-06 16:38:28 +08002010 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002011 return 0;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07002012
2013out:
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002014 vhost_scsi_free_cmd_map_res(tv_nexus, se_sess);
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07002015 transport_free_session(se_sess);
2016 kfree(tv_nexus);
2017 return -ENOMEM;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002018}
2019
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002020static int vhost_scsi_drop_nexus(struct vhost_scsi_tpg *tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002021{
2022 struct se_session *se_sess;
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002023 struct vhost_scsi_nexus *tv_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002024
2025 mutex_lock(&tpg->tv_tpg_mutex);
2026 tv_nexus = tpg->tpg_nexus;
2027 if (!tv_nexus) {
2028 mutex_unlock(&tpg->tv_tpg_mutex);
2029 return -ENODEV;
2030 }
2031
2032 se_sess = tv_nexus->tvn_se_sess;
2033 if (!se_sess) {
2034 mutex_unlock(&tpg->tv_tpg_mutex);
2035 return -ENODEV;
2036 }
2037
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002038 if (tpg->tv_tpg_port_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002039 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002040 pr_err("Unable to remove TCM_vhost I_T Nexus with"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002041 " active TPG port count: %d\n",
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002042 tpg->tv_tpg_port_count);
2043 return -EBUSY;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002044 }
2045
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002046 if (tpg->tv_tpg_vhost_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002047 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002048 pr_err("Unable to remove TCM_vhost I_T Nexus with"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002049 " active TPG vhost count: %d\n",
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002050 tpg->tv_tpg_vhost_count);
2051 return -EBUSY;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002052 }
2053
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002054 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002055 " %s Initiator Port: %s\n", vhost_scsi_dump_proto_id(tpg->tport),
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002056 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07002057
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002058 vhost_scsi_free_cmd_map_res(tv_nexus, se_sess);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002059 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002060 * Release the SCSI I_T Nexus to the emulated vhost Target Port
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002061 */
2062 transport_deregister_session(tv_nexus->tvn_se_sess);
2063 tpg->tpg_nexus = NULL;
2064 mutex_unlock(&tpg->tv_tpg_mutex);
2065
2066 kfree(tv_nexus);
2067 return 0;
2068}
2069
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002070static ssize_t vhost_scsi_tpg_show_nexus(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08002071 char *page)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002072{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002073 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
2074 struct vhost_scsi_tpg, se_tpg);
2075 struct vhost_scsi_nexus *tv_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002076 ssize_t ret;
2077
Asias He98718312013-05-06 16:38:28 +08002078 mutex_lock(&tpg->tv_tpg_mutex);
2079 tv_nexus = tpg->tpg_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002080 if (!tv_nexus) {
Asias He98718312013-05-06 16:38:28 +08002081 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002082 return -ENODEV;
2083 }
2084 ret = snprintf(page, PAGE_SIZE, "%s\n",
2085 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
Asias He98718312013-05-06 16:38:28 +08002086 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002087
2088 return ret;
2089}
2090
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002091static ssize_t vhost_scsi_tpg_store_nexus(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08002092 const char *page,
2093 size_t count)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002094{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002095 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
2096 struct vhost_scsi_tpg, se_tpg);
2097 struct vhost_scsi_tport *tport_wwn = tpg->tport;
2098 unsigned char i_port[VHOST_SCSI_NAMELEN], *ptr, *port_ptr;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002099 int ret;
2100 /*
2101 * Shutdown the active I_T nexus if 'NULL' is passed..
2102 */
2103 if (!strncmp(page, "NULL", 4)) {
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002104 ret = vhost_scsi_drop_nexus(tpg);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002105 return (!ret) ? count : ret;
2106 }
2107 /*
2108 * Otherwise make sure the passed virtual Initiator port WWN matches
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002109 * the fabric protocol_id set in vhost_scsi_make_tport(), and call
2110 * vhost_scsi_make_nexus().
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002111 */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002112 if (strlen(page) >= VHOST_SCSI_NAMELEN) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002113 pr_err("Emulated NAA Sas Address: %s, exceeds"
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002114 " max: %d\n", page, VHOST_SCSI_NAMELEN);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002115 return -EINVAL;
2116 }
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002117 snprintf(&i_port[0], VHOST_SCSI_NAMELEN, "%s", page);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002118
2119 ptr = strstr(i_port, "naa.");
2120 if (ptr) {
2121 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
2122 pr_err("Passed SAS Initiator Port %s does not"
2123 " match target port protoid: %s\n", i_port,
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002124 vhost_scsi_dump_proto_id(tport_wwn));
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002125 return -EINVAL;
2126 }
2127 port_ptr = &i_port[0];
2128 goto check_newline;
2129 }
2130 ptr = strstr(i_port, "fc.");
2131 if (ptr) {
2132 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
2133 pr_err("Passed FCP Initiator Port %s does not"
2134 " match target port protoid: %s\n", i_port,
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002135 vhost_scsi_dump_proto_id(tport_wwn));
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002136 return -EINVAL;
2137 }
2138 port_ptr = &i_port[3]; /* Skip over "fc." */
2139 goto check_newline;
2140 }
2141 ptr = strstr(i_port, "iqn.");
2142 if (ptr) {
2143 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
2144 pr_err("Passed iSCSI Initiator Port %s does not"
2145 " match target port protoid: %s\n", i_port,
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002146 vhost_scsi_dump_proto_id(tport_wwn));
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002147 return -EINVAL;
2148 }
2149 port_ptr = &i_port[0];
2150 goto check_newline;
2151 }
2152 pr_err("Unable to locate prefix for emulated Initiator Port:"
2153 " %s\n", i_port);
2154 return -EINVAL;
2155 /*
2156 * Clear any trailing newline for the NAA WWN
2157 */
2158check_newline:
2159 if (i_port[strlen(i_port)-1] == '\n')
2160 i_port[strlen(i_port)-1] = '\0';
2161
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002162 ret = vhost_scsi_make_nexus(tpg, port_ptr);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002163 if (ret < 0)
2164 return ret;
2165
2166 return count;
2167}
2168
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002169TF_TPG_BASE_ATTR(vhost_scsi, nexus, S_IRUGO | S_IWUSR);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002170
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002171static struct configfs_attribute *vhost_scsi_tpg_attrs[] = {
2172 &vhost_scsi_tpg_nexus.attr,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002173 NULL,
2174};
2175
Asias He683bd962013-05-06 16:38:27 +08002176static struct se_portal_group *
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002177vhost_scsi_make_tpg(struct se_wwn *wwn,
Asias He683bd962013-05-06 16:38:27 +08002178 struct config_group *group,
2179 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002180{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002181 struct vhost_scsi_tport *tport = container_of(wwn,
2182 struct vhost_scsi_tport, tport_wwn);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002183
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002184 struct vhost_scsi_tpg *tpg;
Dan Carpenter59c816c2015-02-05 10:37:33 +03002185 u16 tpgt;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002186 int ret;
2187
2188 if (strstr(name, "tpgt_") != name)
2189 return ERR_PTR(-EINVAL);
Dan Carpenter59c816c2015-02-05 10:37:33 +03002190 if (kstrtou16(name + 5, 10, &tpgt) || tpgt >= VHOST_SCSI_MAX_TARGET)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002191 return ERR_PTR(-EINVAL);
2192
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002193 tpg = kzalloc(sizeof(struct vhost_scsi_tpg), GFP_KERNEL);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002194 if (!tpg) {
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002195 pr_err("Unable to allocate struct vhost_scsi_tpg");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002196 return ERR_PTR(-ENOMEM);
2197 }
2198 mutex_init(&tpg->tv_tpg_mutex);
2199 INIT_LIST_HEAD(&tpg->tv_tpg_list);
2200 tpg->tport = tport;
2201 tpg->tport_tpgt = tpgt;
2202
Christoph Hellwig9ac89282015-04-08 20:01:35 +02002203 ret = core_tpg_register(&vhost_scsi_ops, wwn,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002204 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
2205 if (ret < 0) {
2206 kfree(tpg);
2207 return NULL;
2208 }
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002209 mutex_lock(&vhost_scsi_mutex);
2210 list_add_tail(&tpg->tv_tpg_list, &vhost_scsi_list);
2211 mutex_unlock(&vhost_scsi_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002212
2213 return &tpg->se_tpg;
2214}
2215
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002216static void vhost_scsi_drop_tpg(struct se_portal_group *se_tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002217{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002218 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
2219 struct vhost_scsi_tpg, se_tpg);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002220
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002221 mutex_lock(&vhost_scsi_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002222 list_del(&tpg->tv_tpg_list);
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002223 mutex_unlock(&vhost_scsi_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002224 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002225 * Release the virtual I_T Nexus for this vhost TPG
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002226 */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002227 vhost_scsi_drop_nexus(tpg);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002228 /*
2229 * Deregister the se_tpg from TCM..
2230 */
2231 core_tpg_deregister(se_tpg);
2232 kfree(tpg);
2233}
2234
Asias He683bd962013-05-06 16:38:27 +08002235static struct se_wwn *
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002236vhost_scsi_make_tport(struct target_fabric_configfs *tf,
Asias He683bd962013-05-06 16:38:27 +08002237 struct config_group *group,
2238 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002239{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002240 struct vhost_scsi_tport *tport;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002241 char *ptr;
2242 u64 wwpn = 0;
2243 int off = 0;
2244
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002245 /* if (vhost_scsi_parse_wwn(name, &wwpn, 1) < 0)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002246 return ERR_PTR(-EINVAL); */
2247
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002248 tport = kzalloc(sizeof(struct vhost_scsi_tport), GFP_KERNEL);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002249 if (!tport) {
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002250 pr_err("Unable to allocate struct vhost_scsi_tport");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002251 return ERR_PTR(-ENOMEM);
2252 }
2253 tport->tport_wwpn = wwpn;
2254 /*
2255 * Determine the emulated Protocol Identifier and Target Port Name
2256 * based on the incoming configfs directory name.
2257 */
2258 ptr = strstr(name, "naa.");
2259 if (ptr) {
2260 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
2261 goto check_len;
2262 }
2263 ptr = strstr(name, "fc.");
2264 if (ptr) {
2265 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
2266 off = 3; /* Skip over "fc." */
2267 goto check_len;
2268 }
2269 ptr = strstr(name, "iqn.");
2270 if (ptr) {
2271 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
2272 goto check_len;
2273 }
2274
2275 pr_err("Unable to locate prefix for emulated Target Port:"
2276 " %s\n", name);
2277 kfree(tport);
2278 return ERR_PTR(-EINVAL);
2279
2280check_len:
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002281 if (strlen(name) >= VHOST_SCSI_NAMELEN) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002282 pr_err("Emulated %s Address: %s, exceeds"
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002283 " max: %d\n", name, vhost_scsi_dump_proto_id(tport),
2284 VHOST_SCSI_NAMELEN);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002285 kfree(tport);
2286 return ERR_PTR(-EINVAL);
2287 }
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002288 snprintf(&tport->tport_name[0], VHOST_SCSI_NAMELEN, "%s", &name[off]);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002289
2290 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002291 " %s Address: %s\n", vhost_scsi_dump_proto_id(tport), name);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002292
2293 return &tport->tport_wwn;
2294}
2295
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002296static void vhost_scsi_drop_tport(struct se_wwn *wwn)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002297{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002298 struct vhost_scsi_tport *tport = container_of(wwn,
2299 struct vhost_scsi_tport, tport_wwn);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002300
2301 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002302 " %s Address: %s\n", vhost_scsi_dump_proto_id(tport),
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002303 tport->tport_name);
2304
2305 kfree(tport);
2306}
2307
Asias He683bd962013-05-06 16:38:27 +08002308static ssize_t
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002309vhost_scsi_wwn_show_attr_version(struct target_fabric_configfs *tf,
Asias He683bd962013-05-06 16:38:27 +08002310 char *page)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002311{
2312 return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002313 "on "UTS_RELEASE"\n", VHOST_SCSI_VERSION, utsname()->sysname,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002314 utsname()->machine);
2315}
2316
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002317TF_WWN_ATTR_RO(vhost_scsi, version);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002318
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002319static struct configfs_attribute *vhost_scsi_wwn_attrs[] = {
2320 &vhost_scsi_wwn_version.attr,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002321 NULL,
2322};
2323
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002324static struct target_core_fabric_ops vhost_scsi_ops = {
Christoph Hellwig9ac89282015-04-08 20:01:35 +02002325 .module = THIS_MODULE,
2326 .name = "vhost",
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002327 .get_fabric_name = vhost_scsi_get_fabric_name,
2328 .get_fabric_proto_ident = vhost_scsi_get_fabric_proto_ident,
2329 .tpg_get_wwn = vhost_scsi_get_fabric_wwn,
2330 .tpg_get_tag = vhost_scsi_get_tpgt,
2331 .tpg_get_default_depth = vhost_scsi_get_default_depth,
2332 .tpg_get_pr_transport_id = vhost_scsi_get_pr_transport_id,
2333 .tpg_get_pr_transport_id_len = vhost_scsi_get_pr_transport_id_len,
2334 .tpg_parse_pr_out_transport_id = vhost_scsi_parse_pr_out_transport_id,
2335 .tpg_check_demo_mode = vhost_scsi_check_true,
2336 .tpg_check_demo_mode_cache = vhost_scsi_check_true,
2337 .tpg_check_demo_mode_write_protect = vhost_scsi_check_false,
2338 .tpg_check_prod_mode_write_protect = vhost_scsi_check_false,
Nicholas Bellingerb1d75fe2015-03-28 00:03:51 -07002339 .tpg_check_prot_fabric_only = vhost_scsi_check_prot_fabric_only,
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002340 .tpg_alloc_fabric_acl = vhost_scsi_alloc_fabric_acl,
2341 .tpg_release_fabric_acl = vhost_scsi_release_fabric_acl,
2342 .tpg_get_inst_index = vhost_scsi_tpg_get_inst_index,
2343 .release_cmd = vhost_scsi_release_cmd,
Nicholas Bellinger084ed452013-06-06 02:20:41 -07002344 .check_stop_free = vhost_scsi_check_stop_free,
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002345 .shutdown_session = vhost_scsi_shutdown_session,
2346 .close_session = vhost_scsi_close_session,
2347 .sess_get_index = vhost_scsi_sess_get_index,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002348 .sess_get_initiator_sid = NULL,
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002349 .write_pending = vhost_scsi_write_pending,
2350 .write_pending_status = vhost_scsi_write_pending_status,
2351 .set_default_node_attributes = vhost_scsi_set_default_node_attrs,
2352 .get_task_tag = vhost_scsi_get_task_tag,
2353 .get_cmd_state = vhost_scsi_get_cmd_state,
2354 .queue_data_in = vhost_scsi_queue_data_in,
2355 .queue_status = vhost_scsi_queue_status,
2356 .queue_tm_rsp = vhost_scsi_queue_tm_rsp,
2357 .aborted_task = vhost_scsi_aborted_task,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002358 /*
2359 * Setup callers for generic logic in target_core_fabric_configfs.c
2360 */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002361 .fabric_make_wwn = vhost_scsi_make_tport,
2362 .fabric_drop_wwn = vhost_scsi_drop_tport,
2363 .fabric_make_tpg = vhost_scsi_make_tpg,
2364 .fabric_drop_tpg = vhost_scsi_drop_tpg,
2365 .fabric_post_link = vhost_scsi_port_link,
2366 .fabric_pre_unlink = vhost_scsi_port_unlink,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002367 .fabric_make_np = NULL,
2368 .fabric_drop_np = NULL,
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002369 .fabric_make_nodeacl = vhost_scsi_make_nodeacl,
2370 .fabric_drop_nodeacl = vhost_scsi_drop_nodeacl,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002371
Christoph Hellwig9ac89282015-04-08 20:01:35 +02002372 .tfc_wwn_attrs = vhost_scsi_wwn_attrs,
2373 .tfc_tpg_base_attrs = vhost_scsi_tpg_attrs,
2374 .tfc_tpg_attrib_attrs = vhost_scsi_tpg_attrib_attrs,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002375};
2376
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002377static int __init vhost_scsi_init(void)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002378{
2379 int ret = -ENOMEM;
Christoph Hellwig9ac89282015-04-08 20:01:35 +02002380
2381 pr_debug("TCM_VHOST fabric module %s on %s/%s"
2382 " on "UTS_RELEASE"\n", VHOST_SCSI_VERSION, utsname()->sysname,
2383 utsname()->machine);
2384
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002385 /*
2386 * Use our own dedicated workqueue for submitting I/O into
2387 * target core to avoid contention within system_wq.
2388 */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002389 vhost_scsi_workqueue = alloc_workqueue("vhost_scsi", 0, 0);
2390 if (!vhost_scsi_workqueue)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002391 goto out;
2392
2393 ret = vhost_scsi_register();
2394 if (ret < 0)
2395 goto out_destroy_workqueue;
2396
Christoph Hellwig9ac89282015-04-08 20:01:35 +02002397 ret = target_register_template(&vhost_scsi_ops);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002398 if (ret < 0)
2399 goto out_vhost_scsi_deregister;
2400
2401 return 0;
2402
2403out_vhost_scsi_deregister:
2404 vhost_scsi_deregister();
2405out_destroy_workqueue:
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002406 destroy_workqueue(vhost_scsi_workqueue);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002407out:
2408 return ret;
2409};
2410
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002411static void vhost_scsi_exit(void)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002412{
Christoph Hellwig9ac89282015-04-08 20:01:35 +02002413 target_unregister_template(&vhost_scsi_ops);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002414 vhost_scsi_deregister();
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002415 destroy_workqueue(vhost_scsi_workqueue);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002416};
2417
Michael S. Tsirkin181c04a2013-05-02 03:52:59 +03002418MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
2419MODULE_ALIAS("tcm_vhost");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002420MODULE_LICENSE("GPL");
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002421module_init(vhost_scsi_init);
2422module_exit(vhost_scsi_exit);