blob: e31339be7dd78821ea17754e9c6fbd4f60c2da1b [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>
David S. Miller5538d292015-05-28 11:35:41 -070038#include <linux/vmalloc.h>
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070039#include <linux/miscdevice.h>
40#include <asm/unaligned.h>
Bart Van Asscheba929992015-05-08 10:11:12 +020041#include <scsi/scsi_common.h>
42#include <scsi/scsi_proto.h>
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070043#include <target/target_core_base.h>
44#include <target/target_core_fabric.h>
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070045#include <linux/vhost.h>
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070046#include <linux/virtio_scsi.h>
Asias He9d6064a2013-01-06 14:36:13 +080047#include <linux/llist.h>
Asias He1b7f3902013-02-06 13:20:59 +080048#include <linux/bitmap.h>
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070049
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070050#include "vhost.h"
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +030051
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -080052#define VHOST_SCSI_VERSION "v0.1"
53#define VHOST_SCSI_NAMELEN 256
54#define VHOST_SCSI_MAX_CDB_SIZE 32
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -080055#define VHOST_SCSI_PREALLOC_SGLS 2048
56#define VHOST_SCSI_PREALLOC_UPAGES 2048
Greg Edwards864d39d2018-08-08 13:29:55 -060057#define VHOST_SCSI_PREALLOC_PROT_SGLS 2048
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +030058
Jason Wange82b9b02019-05-17 00:29:49 -040059/* Max number of requests before requeueing the job.
60 * Using this limit prevents one virtqueue from starving others with
61 * request.
62 */
63#define VHOST_SCSI_WEIGHT 256
64
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +030065struct vhost_scsi_inflight {
66 /* Wait for the flush operation to finish */
67 struct completion comp;
68 /* Refcount for the inflight reqs */
69 struct kref kref;
70};
71
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -080072struct vhost_scsi_cmd {
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +030073 /* Descriptor from vhost_get_vq_desc() for virt_queue segment */
74 int tvc_vq_desc;
75 /* virtio-scsi initiator task attribute */
76 int tvc_task_attr;
Nicholas Bellinger79c14142015-01-27 13:13:12 -080077 /* virtio-scsi response incoming iovecs */
78 int tvc_in_iovs;
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +030079 /* virtio-scsi initiator data direction */
80 enum dma_data_direction tvc_data_direction;
81 /* Expected data transfer length from virtio-scsi header */
82 u32 tvc_exp_data_len;
83 /* The Tag from include/linux/virtio_scsi.h:struct virtio_scsi_cmd_req */
84 u64 tvc_tag;
85 /* The number of scatterlists associated with this cmd */
86 u32 tvc_sgl_count;
Nicholas Bellingere31885d2014-02-22 18:34:43 -080087 u32 tvc_prot_sgl_count;
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -080088 /* Saved unpacked SCSI LUN for vhost_scsi_submission_work() */
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +030089 u32 tvc_lun;
90 /* Pointer to the SGL formatted memory from virtio-scsi */
91 struct scatterlist *tvc_sgl;
Nicholas Bellingerb1935f62014-02-22 18:08:24 -080092 struct scatterlist *tvc_prot_sgl;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -070093 struct page **tvc_upages;
Nicholas Bellinger79c14142015-01-27 13:13:12 -080094 /* Pointer to response header iovec */
Benjamin Coddingtona77ec832016-06-06 18:07:59 -040095 struct iovec tvc_resp_iov;
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +030096 /* Pointer to vhost_scsi for our device */
97 struct vhost_scsi *tvc_vhost;
98 /* Pointer to vhost_virtqueue for the cmd */
99 struct vhost_virtqueue *tvc_vq;
100 /* Pointer to vhost nexus memory */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800101 struct vhost_scsi_nexus *tvc_nexus;
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300102 /* The TCM I/O descriptor that is accessed via container_of() */
103 struct se_cmd tvc_se_cmd;
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800104 /* work item used for cmwq dispatch to vhost_scsi_submission_work() */
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300105 struct work_struct work;
106 /* Copy of the incoming SCSI command descriptor block (CDB) */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800107 unsigned char tvc_cdb[VHOST_SCSI_MAX_CDB_SIZE];
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300108 /* Sense buffer that will be mapped into outgoing status */
109 unsigned char tvc_sense_buf[TRANSPORT_SENSE_BUFFER];
110 /* Completed commands list, serviced from vhost worker thread */
111 struct llist_node tvc_completion_list;
112 /* Used to track inflight cmd */
113 struct vhost_scsi_inflight *inflight;
114};
115
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800116struct vhost_scsi_nexus {
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300117 /* Pointer to TCM session for I_T Nexus */
118 struct se_session *tvn_se_sess;
119};
120
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800121struct vhost_scsi_tpg {
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300122 /* Vhost port target portal group tag for TCM */
123 u16 tport_tpgt;
124 /* Used to track number of TPG Port/Lun Links wrt to explict I_T Nexus shutdown */
125 int tv_tpg_port_count;
126 /* Used for vhost_scsi device reference to tpg_nexus, protected by tv_tpg_mutex */
127 int tv_tpg_vhost_count;
Nicholas Bellingerb1d75fe2015-03-28 00:03:51 -0700128 /* Used for enabling T10-PI with legacy devices */
129 int tv_fabric_prot_type;
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800130 /* list for vhost_scsi_list */
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300131 struct list_head tv_tpg_list;
132 /* Used to protect access for tpg_nexus */
133 struct mutex tv_tpg_mutex;
134 /* Pointer to the TCM VHost I_T Nexus for this TPG endpoint */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800135 struct vhost_scsi_nexus *tpg_nexus;
136 /* Pointer back to vhost_scsi_tport */
137 struct vhost_scsi_tport *tport;
138 /* Returned by vhost_scsi_make_tpg() */
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300139 struct se_portal_group se_tpg;
140 /* Pointer back to vhost_scsi, protected by tv_tpg_mutex */
141 struct vhost_scsi *vhost_scsi;
142};
143
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800144struct vhost_scsi_tport {
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300145 /* SCSI protocol the tport is providing */
146 u8 tport_proto_id;
147 /* Binary World Wide unique Port Name for Vhost Target port */
148 u64 tport_wwpn;
149 /* ASCII formatted WWPN for Vhost Target port */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800150 char tport_name[VHOST_SCSI_NAMELEN];
151 /* Returned by vhost_scsi_make_tport() */
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300152 struct se_wwn tport_wwn;
153};
154
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800155struct vhost_scsi_evt {
Michael S. Tsirkin5012a3a2013-05-02 03:50:34 +0300156 /* event to be sent to guest */
157 struct virtio_scsi_event event;
158 /* event list, serviced from vhost worker thread */
159 struct llist_node list;
160};
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700161
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700162enum {
163 VHOST_SCSI_VQ_CTL = 0,
164 VHOST_SCSI_VQ_EVT = 1,
165 VHOST_SCSI_VQ_IO = 2,
166};
167
Michael S. Tsirkine72fd722014-11-23 18:01:34 +0200168/* Note: can't set VIRTIO_F_VERSION_1 yet, since that implies ANY_LAYOUT. */
Nicholas Bellinger5dade712013-03-27 17:23:41 -0700169enum {
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800170 VHOST_SCSI_FEATURES = VHOST_FEATURES | (1ULL << VIRTIO_SCSI_F_HOTPLUG) |
Michael S. Tsirkin4e9fa502015-09-09 22:24:56 +0300171 (1ULL << VIRTIO_SCSI_F_T10_PI)
Nicholas Bellinger5dade712013-03-27 17:23:41 -0700172};
173
Asias He1b7f3902013-02-06 13:20:59 +0800174#define VHOST_SCSI_MAX_TARGET 256
175#define VHOST_SCSI_MAX_VQ 128
Asias Hea6c9af82013-04-25 15:35:21 +0800176#define VHOST_SCSI_MAX_EVENT 128
Asias He67e18cf2013-02-05 12:31:57 +0800177
Asias He3ab2e422013-04-27 11:16:48 +0800178struct vhost_scsi_virtqueue {
179 struct vhost_virtqueue vq;
Michael S. Tsirkin3dfbff32013-04-28 15:38:52 +0300180 /*
181 * Reference counting for inflight reqs, used for flush operation. At
182 * each time, one reference tracks new commands submitted, while we
183 * wait for another one to reach 0.
184 */
Asias Hef2f0173d2013-04-27 11:16:49 +0800185 struct vhost_scsi_inflight inflights[2];
Michael S. Tsirkin3dfbff32013-04-28 15:38:52 +0300186 /*
187 * Indicate current inflight in use, protected by vq->mutex.
188 * Writers must also take dev mutex and flush under it.
189 */
Asias Hef2f0173d2013-04-27 11:16:49 +0800190 int inflight_idx;
Mike Christie25b98b62020-11-09 23:33:20 -0600191 struct vhost_scsi_cmd *scsi_cmds;
192 struct sbitmap scsi_tags;
193 int max_cmds;
Asias He3ab2e422013-04-27 11:16:48 +0800194};
195
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700196struct vhost_scsi {
Asias He67e18cf2013-02-05 12:31:57 +0800197 /* Protected by vhost_scsi->dev.mutex */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800198 struct vhost_scsi_tpg **vs_tpg;
Asias He67e18cf2013-02-05 12:31:57 +0800199 char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
Asias He67e18cf2013-02-05 12:31:57 +0800200
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700201 struct vhost_dev dev;
Asias He3ab2e422013-04-27 11:16:48 +0800202 struct vhost_scsi_virtqueue vqs[VHOST_SCSI_MAX_VQ];
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700203
204 struct vhost_work vs_completion_work; /* cmd completion work item */
Asias He9d6064a2013-01-06 14:36:13 +0800205 struct llist_head vs_completion_list; /* cmd completion queue */
Asias Hea6c9af82013-04-25 15:35:21 +0800206
207 struct vhost_work vs_event_work; /* evt injection work item */
208 struct llist_head vs_event_list; /* evt injection queue */
209
210 bool vs_events_missed; /* any missed events, protected by vq->mutex */
211 int vs_events_nr; /* num of pending events, protected by vq->mutex */
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700212};
213
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -0700214/*
215 * Context for processing request and control queue operations.
216 */
217struct vhost_scsi_ctx {
218 int head;
219 unsigned int out, in;
220 size_t req_size, rsp_size;
221 size_t out_size, in_size;
222 u8 *target, *lunp;
223 void *req;
224 struct iov_iter out_iter;
225};
226
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800227static struct workqueue_struct *vhost_scsi_workqueue;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700228
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800229/* Global spinlock to protect vhost_scsi TPG list for vhost IOCTL access */
230static DEFINE_MUTEX(vhost_scsi_mutex);
231static LIST_HEAD(vhost_scsi_list);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700232
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800233static void vhost_scsi_done_inflight(struct kref *kref)
Asias Hef2f0173d2013-04-27 11:16:49 +0800234{
235 struct vhost_scsi_inflight *inflight;
236
237 inflight = container_of(kref, struct vhost_scsi_inflight, kref);
238 complete(&inflight->comp);
239}
240
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800241static void vhost_scsi_init_inflight(struct vhost_scsi *vs,
Asias Hef2f0173d2013-04-27 11:16:49 +0800242 struct vhost_scsi_inflight *old_inflight[])
243{
244 struct vhost_scsi_inflight *new_inflight;
245 struct vhost_virtqueue *vq;
246 int idx, i;
247
248 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
249 vq = &vs->vqs[i].vq;
250
251 mutex_lock(&vq->mutex);
252
253 /* store old infight */
254 idx = vs->vqs[i].inflight_idx;
255 if (old_inflight)
256 old_inflight[i] = &vs->vqs[i].inflights[idx];
257
258 /* setup new infight */
259 vs->vqs[i].inflight_idx = idx ^ 1;
260 new_inflight = &vs->vqs[i].inflights[idx ^ 1];
261 kref_init(&new_inflight->kref);
262 init_completion(&new_inflight->comp);
263
264 mutex_unlock(&vq->mutex);
265 }
266}
267
268static struct vhost_scsi_inflight *
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800269vhost_scsi_get_inflight(struct vhost_virtqueue *vq)
Asias Hef2f0173d2013-04-27 11:16:49 +0800270{
271 struct vhost_scsi_inflight *inflight;
272 struct vhost_scsi_virtqueue *svq;
273
274 svq = container_of(vq, struct vhost_scsi_virtqueue, vq);
275 inflight = &svq->inflights[svq->inflight_idx];
276 kref_get(&inflight->kref);
277
278 return inflight;
279}
280
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800281static void vhost_scsi_put_inflight(struct vhost_scsi_inflight *inflight)
Asias Hef2f0173d2013-04-27 11:16:49 +0800282{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800283 kref_put(&inflight->kref, vhost_scsi_done_inflight);
Asias Hef2f0173d2013-04-27 11:16:49 +0800284}
285
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800286static int vhost_scsi_check_true(struct se_portal_group *se_tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700287{
288 return 1;
289}
290
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800291static int vhost_scsi_check_false(struct se_portal_group *se_tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700292{
293 return 0;
294}
295
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800296static char *vhost_scsi_get_fabric_wwn(struct se_portal_group *se_tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700297{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800298 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
299 struct vhost_scsi_tpg, se_tpg);
300 struct vhost_scsi_tport *tport = tpg->tport;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700301
302 return &tport->tport_name[0];
303}
304
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800305static u16 vhost_scsi_get_tpgt(struct se_portal_group *se_tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700306{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800307 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
308 struct vhost_scsi_tpg, se_tpg);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700309 return tpg->tport_tpgt;
310}
311
Nicholas Bellingerb1d75fe2015-03-28 00:03:51 -0700312static int vhost_scsi_check_prot_fabric_only(struct se_portal_group *se_tpg)
313{
314 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
315 struct vhost_scsi_tpg, se_tpg);
316
317 return tpg->tv_fabric_prot_type;
318}
319
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800320static u32 vhost_scsi_tpg_get_inst_index(struct se_portal_group *se_tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700321{
322 return 1;
323}
324
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800325static void vhost_scsi_release_cmd(struct se_cmd *se_cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700326{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800327 struct vhost_scsi_cmd *tv_cmd = container_of(se_cmd,
328 struct vhost_scsi_cmd, tvc_se_cmd);
Mike Christie25b98b62020-11-09 23:33:20 -0600329 struct vhost_scsi_virtqueue *svq = container_of(tv_cmd->tvc_vq,
330 struct vhost_scsi_virtqueue, vq);
331 struct vhost_scsi_inflight *inflight = tv_cmd->inflight;
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800332 int i;
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700333
334 if (tv_cmd->tvc_sgl_count) {
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700335 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
336 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
Michael S. Tsirkind3d665a2013-09-17 22:54:31 +0300337 }
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800338 if (tv_cmd->tvc_prot_sgl_count) {
339 for (i = 0; i < tv_cmd->tvc_prot_sgl_count; i++)
340 put_page(sg_page(&tv_cmd->tvc_prot_sgl[i]));
341 }
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700342
Mike Christie25b98b62020-11-09 23:33:20 -0600343 sbitmap_clear_bit(&svq->scsi_tags, se_cmd->map_tag);
344 vhost_scsi_put_inflight(inflight);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700345}
346
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800347static u32 vhost_scsi_sess_get_index(struct se_session *se_sess)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700348{
349 return 0;
350}
351
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800352static int vhost_scsi_write_pending(struct se_cmd *se_cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700353{
354 /* Go ahead and process the write immediately */
355 target_execute_cmd(se_cmd);
356 return 0;
357}
358
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800359static void vhost_scsi_set_default_node_attrs(struct se_node_acl *nacl)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700360{
361 return;
362}
363
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800364static int vhost_scsi_get_cmd_state(struct se_cmd *se_cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700365{
366 return 0;
367}
368
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800369static void vhost_scsi_complete_cmd(struct vhost_scsi_cmd *cmd)
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700370{
Asias He3c63f662013-05-06 16:38:29 +0800371 struct vhost_scsi *vs = cmd->tvc_vhost;
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700372
Asias He3c63f662013-05-06 16:38:29 +0800373 llist_add(&cmd->tvc_completion_list, &vs->vs_completion_list);
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700374
375 vhost_work_queue(&vs->dev, &vs->vs_completion_work);
376}
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700377
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800378static int vhost_scsi_queue_data_in(struct se_cmd *se_cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700379{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800380 struct vhost_scsi_cmd *cmd = container_of(se_cmd,
381 struct vhost_scsi_cmd, tvc_se_cmd);
Asias He3c63f662013-05-06 16:38:29 +0800382 vhost_scsi_complete_cmd(cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700383 return 0;
384}
385
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800386static int vhost_scsi_queue_status(struct se_cmd *se_cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700387{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800388 struct vhost_scsi_cmd *cmd = container_of(se_cmd,
389 struct vhost_scsi_cmd, tvc_se_cmd);
Asias He3c63f662013-05-06 16:38:29 +0800390 vhost_scsi_complete_cmd(cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700391 return 0;
392}
393
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800394static void vhost_scsi_queue_tm_rsp(struct se_cmd *se_cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700395{
Joern Engelb79fafa2013-07-03 11:22:17 -0400396 return;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700397}
398
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800399static void vhost_scsi_aborted_task(struct se_cmd *se_cmd)
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700400{
401 return;
402}
403
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800404static void vhost_scsi_free_evt(struct vhost_scsi *vs, struct vhost_scsi_evt *evt)
Asias Hea6c9af82013-04-25 15:35:21 +0800405{
406 vs->vs_events_nr--;
407 kfree(evt);
408}
409
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800410static struct vhost_scsi_evt *
411vhost_scsi_allocate_evt(struct vhost_scsi *vs,
Asias He683bd962013-05-06 16:38:27 +0800412 u32 event, u32 reason)
Asias Hea6c9af82013-04-25 15:35:21 +0800413{
Asias He3ab2e422013-04-27 11:16:48 +0800414 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800415 struct vhost_scsi_evt *evt;
Asias Hea6c9af82013-04-25 15:35:21 +0800416
417 if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) {
418 vs->vs_events_missed = true;
419 return NULL;
420 }
421
422 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
423 if (!evt) {
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800424 vq_err(vq, "Failed to allocate vhost_scsi_evt\n");
Asias Hea6c9af82013-04-25 15:35:21 +0800425 vs->vs_events_missed = true;
426 return NULL;
427 }
428
Michael S. Tsirkine72fd722014-11-23 18:01:34 +0200429 evt->event.event = cpu_to_vhost32(vq, event);
430 evt->event.reason = cpu_to_vhost32(vq, reason);
Asias Hea6c9af82013-04-25 15:35:21 +0800431 vs->vs_events_nr++;
432
433 return evt;
434}
435
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800436static void vhost_scsi_free_cmd(struct vhost_scsi_cmd *cmd)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700437{
Asias He3c63f662013-05-06 16:38:29 +0800438 struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700439
440 /* TODO locking against target/backend threads? */
Nicholas Bellinger6c131d02013-06-06 01:44:48 -0700441 transport_generic_free_cmd(se_cmd, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700442
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700443}
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700444
Nicholas Bellinger084ed452013-06-06 02:20:41 -0700445static int vhost_scsi_check_stop_free(struct se_cmd *se_cmd)
446{
Bart Van Asscheafc16602015-04-27 13:52:36 +0200447 return target_put_sess_cmd(se_cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700448}
449
Asias He683bd962013-05-06 16:38:27 +0800450static void
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800451vhost_scsi_do_evt_work(struct vhost_scsi *vs, struct vhost_scsi_evt *evt)
Asias Hea6c9af82013-04-25 15:35:21 +0800452{
Asias He3ab2e422013-04-27 11:16:48 +0800453 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Asias Hea6c9af82013-04-25 15:35:21 +0800454 struct virtio_scsi_event *event = &evt->event;
455 struct virtio_scsi_event __user *eventp;
456 unsigned out, in;
457 int head, ret;
458
Eugenio Pérez247643f2020-03-31 21:27:57 +0200459 if (!vhost_vq_get_backend(vq)) {
Asias Hea6c9af82013-04-25 15:35:21 +0800460 vs->vs_events_missed = true;
461 return;
462 }
463
464again:
465 vhost_disable_notify(&vs->dev, vq);
Michael S. Tsirkin47283be2014-06-05 15:20:27 +0300466 head = vhost_get_vq_desc(vq, vq->iov,
Asias Hea6c9af82013-04-25 15:35:21 +0800467 ARRAY_SIZE(vq->iov), &out, &in,
468 NULL, NULL);
469 if (head < 0) {
470 vs->vs_events_missed = true;
471 return;
472 }
473 if (head == vq->num) {
474 if (vhost_enable_notify(&vs->dev, vq))
475 goto again;
476 vs->vs_events_missed = true;
477 return;
478 }
479
480 if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) {
481 vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n",
482 vq->iov[out].iov_len);
483 vs->vs_events_missed = true;
484 return;
485 }
486
487 if (vs->vs_events_missed) {
Michael S. Tsirkine72fd722014-11-23 18:01:34 +0200488 event->event |= cpu_to_vhost32(vq, VIRTIO_SCSI_T_EVENTS_MISSED);
Asias Hea6c9af82013-04-25 15:35:21 +0800489 vs->vs_events_missed = false;
490 }
491
492 eventp = vq->iov[out].iov_base;
493 ret = __copy_to_user(eventp, event, sizeof(*event));
494 if (!ret)
495 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
496 else
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800497 vq_err(vq, "Faulted on vhost_scsi_send_event\n");
Asias Hea6c9af82013-04-25 15:35:21 +0800498}
499
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800500static void vhost_scsi_evt_work(struct vhost_work *work)
Asias Hea6c9af82013-04-25 15:35:21 +0800501{
502 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
503 vs_event_work);
Asias He3ab2e422013-04-27 11:16:48 +0800504 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Byungchul Park12bdcbd2017-05-12 09:42:56 +0900505 struct vhost_scsi_evt *evt, *t;
Asias Hea6c9af82013-04-25 15:35:21 +0800506 struct llist_node *llnode;
507
508 mutex_lock(&vq->mutex);
509 llnode = llist_del_all(&vs->vs_event_list);
Byungchul Park12bdcbd2017-05-12 09:42:56 +0900510 llist_for_each_entry_safe(evt, t, llnode, list) {
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800511 vhost_scsi_do_evt_work(vs, evt);
512 vhost_scsi_free_evt(vs, evt);
Asias Hea6c9af82013-04-25 15:35:21 +0800513 }
514 mutex_unlock(&vq->mutex);
515}
516
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700517/* Fill in status and signal that we are done processing this command
518 *
519 * This is scheduled in the vhost work queue so we are called with the owner
520 * process mm and can access the vring.
521 */
522static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
523{
524 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
525 vs_completion_work);
Asias He1b7f3902013-02-06 13:20:59 +0800526 DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
Asias He9d6064a2013-01-06 14:36:13 +0800527 struct virtio_scsi_cmd_resp v_rsp;
Byungchul Park816e85e2017-11-09 09:00:21 +0900528 struct vhost_scsi_cmd *cmd, *t;
Asias He9d6064a2013-01-06 14:36:13 +0800529 struct llist_node *llnode;
530 struct se_cmd *se_cmd;
Nicholas Bellinger79c14142015-01-27 13:13:12 -0800531 struct iov_iter iov_iter;
Asias He1b7f3902013-02-06 13:20:59 +0800532 int ret, vq;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700533
Asias He1b7f3902013-02-06 13:20:59 +0800534 bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
Asias He9d6064a2013-01-06 14:36:13 +0800535 llnode = llist_del_all(&vs->vs_completion_list);
Byungchul Park816e85e2017-11-09 09:00:21 +0900536 llist_for_each_entry_safe(cmd, t, llnode, tvc_completion_list) {
Asias He3c63f662013-05-06 16:38:29 +0800537 se_cmd = &cmd->tvc_se_cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700538
539 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
Asias He3c63f662013-05-06 16:38:29 +0800540 cmd, se_cmd->residual_count, se_cmd->scsi_status);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700541
542 memset(&v_rsp, 0, sizeof(v_rsp));
Michael S. Tsirkine72fd722014-11-23 18:01:34 +0200543 v_rsp.resid = cpu_to_vhost32(cmd->tvc_vq, se_cmd->residual_count);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700544 /* TODO is status_qualifier field needed? */
545 v_rsp.status = se_cmd->scsi_status;
Michael S. Tsirkine72fd722014-11-23 18:01:34 +0200546 v_rsp.sense_len = cpu_to_vhost32(cmd->tvc_vq,
547 se_cmd->scsi_sense_length);
Asias He3c63f662013-05-06 16:38:29 +0800548 memcpy(v_rsp.sense, cmd->tvc_sense_buf,
Michael S. Tsirkine72fd722014-11-23 18:01:34 +0200549 se_cmd->scsi_sense_length);
Nicholas Bellinger79c14142015-01-27 13:13:12 -0800550
Benjamin Coddingtona77ec832016-06-06 18:07:59 -0400551 iov_iter_init(&iov_iter, READ, &cmd->tvc_resp_iov,
Nicholas Bellinger79c14142015-01-27 13:13:12 -0800552 cmd->tvc_in_iovs, sizeof(v_rsp));
553 ret = copy_to_iter(&v_rsp, sizeof(v_rsp), &iov_iter);
554 if (likely(ret == sizeof(v_rsp))) {
Asias He3ab2e422013-04-27 11:16:48 +0800555 struct vhost_scsi_virtqueue *q;
Asias He3c63f662013-05-06 16:38:29 +0800556 vhost_add_used(cmd->tvc_vq, cmd->tvc_vq_desc, 0);
557 q = container_of(cmd->tvc_vq, struct vhost_scsi_virtqueue, vq);
Asias He3ab2e422013-04-27 11:16:48 +0800558 vq = q - vs->vqs;
Asias He1b7f3902013-02-06 13:20:59 +0800559 __set_bit(vq, signal);
560 } else
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700561 pr_err("Faulted on virtio_scsi_cmd_resp\n");
562
Asias He3c63f662013-05-06 16:38:29 +0800563 vhost_scsi_free_cmd(cmd);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700564 }
565
Asias He1b7f3902013-02-06 13:20:59 +0800566 vq = -1;
567 while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1))
568 < VHOST_SCSI_MAX_VQ)
Asias He3ab2e422013-04-27 11:16:48 +0800569 vhost_signal(&vs->dev, &vs->vqs[vq].vq);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700570}
571
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800572static struct vhost_scsi_cmd *
Mike Christie25b98b62020-11-09 23:33:20 -0600573vhost_scsi_get_cmd(struct vhost_virtqueue *vq, struct vhost_scsi_tpg *tpg,
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800574 unsigned char *cdb, u64 scsi_tag, u16 lun, u8 task_attr,
575 u32 exp_data_len, int data_direction)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700576{
Mike Christie25b98b62020-11-09 23:33:20 -0600577 struct vhost_scsi_virtqueue *svq = container_of(vq,
578 struct vhost_scsi_virtqueue, vq);
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800579 struct vhost_scsi_cmd *cmd;
580 struct vhost_scsi_nexus *tv_nexus;
Nicholas Bellingerb1935f62014-02-22 18:08:24 -0800581 struct scatterlist *sg, *prot_sg;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700582 struct page **pages;
Mike Christie25b98b62020-11-09 23:33:20 -0600583 int tag;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700584
Asias He98718312013-05-06 16:38:28 +0800585 tv_nexus = tpg->tpg_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700586 if (!tv_nexus) {
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800587 pr_err("Unable to locate active struct vhost_scsi_nexus\n");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700588 return ERR_PTR(-EIO);
589 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700590
Mike Christie25b98b62020-11-09 23:33:20 -0600591 tag = sbitmap_get(&svq->scsi_tags, 0, false);
Nicholas Bellinger4a47d3a2013-09-23 11:42:28 -0700592 if (tag < 0) {
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800593 pr_err("Unable to obtain tag for vhost_scsi_cmd\n");
Nicholas Bellinger4a47d3a2013-09-23 11:42:28 -0700594 return ERR_PTR(-ENOMEM);
595 }
596
Mike Christie25b98b62020-11-09 23:33:20 -0600597 cmd = &svq->scsi_cmds[tag];
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700598 sg = cmd->tvc_sgl;
Nicholas Bellingerb1935f62014-02-22 18:08:24 -0800599 prot_sg = cmd->tvc_prot_sgl;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700600 pages = cmd->tvc_upages;
Markus Elfring473f0b12017-05-20 13:48:44 +0200601 memset(cmd, 0, sizeof(*cmd));
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700602 cmd->tvc_sgl = sg;
Nicholas Bellingerb1935f62014-02-22 18:08:24 -0800603 cmd->tvc_prot_sgl = prot_sg;
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700604 cmd->tvc_upages = pages;
Nicholas Bellinger4824d3b2013-06-07 17:47:46 -0700605 cmd->tvc_se_cmd.map_tag = tag;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800606 cmd->tvc_tag = scsi_tag;
607 cmd->tvc_lun = lun;
608 cmd->tvc_task_attr = task_attr;
Asias He3c63f662013-05-06 16:38:29 +0800609 cmd->tvc_exp_data_len = exp_data_len;
610 cmd->tvc_data_direction = data_direction;
611 cmd->tvc_nexus = tv_nexus;
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800612 cmd->inflight = vhost_scsi_get_inflight(vq);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700613
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800614 memcpy(cmd->tvc_cdb, cdb, VHOST_SCSI_MAX_CDB_SIZE);
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800615
Asias He3c63f662013-05-06 16:38:29 +0800616 return cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700617}
618
619/*
620 * Map a user memory range into a scatterlist
621 *
622 * Returns the number of scatterlist entries used or -errno on error.
623 */
Asias He683bd962013-05-06 16:38:27 +0800624static int
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800625vhost_scsi_map_to_sgl(struct vhost_scsi_cmd *cmd,
Al Viro2f240c42017-09-24 22:07:59 -0400626 struct iov_iter *iter,
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -0700627 struct scatterlist *sgl,
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800628 bool write)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700629{
Nicholas Bellingerb4078b52015-01-28 13:10:51 -0800630 struct page **pages = cmd->tvc_upages;
Al Viro2f240c42017-09-24 22:07:59 -0400631 struct scatterlist *sg = sgl;
632 ssize_t bytes;
633 size_t offset;
634 unsigned int npages = 0;
Asias He18100532013-01-22 11:20:27 +0800635
Al Viro2f240c42017-09-24 22:07:59 -0400636 bytes = iov_iter_get_pages(iter, pages, LONG_MAX,
637 VHOST_SCSI_PREALLOC_UPAGES, &offset);
Asias He18100532013-01-22 11:20:27 +0800638 /* No pages were pinned */
Al Viro2f240c42017-09-24 22:07:59 -0400639 if (bytes <= 0)
640 return bytes < 0 ? bytes : -EFAULT;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700641
Al Viro2f240c42017-09-24 22:07:59 -0400642 iov_iter_advance(iter, bytes);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700643
Al Viro2f240c42017-09-24 22:07:59 -0400644 while (bytes) {
645 unsigned n = min_t(unsigned, PAGE_SIZE - offset, bytes);
646 sg_set_page(sg++, pages[npages++], n, offset);
647 bytes -= n;
648 offset = 0;
649 }
650 return npages;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700651}
652
Asias He683bd962013-05-06 16:38:27 +0800653static int
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800654vhost_scsi_calc_sgls(struct iov_iter *iter, size_t bytes, int max_sgls)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700655{
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800656 int sgl_count = 0;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700657
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800658 if (!iter || !iter->iov) {
659 pr_err("%s: iter->iov is NULL, but expected bytes: %zu"
660 " present\n", __func__, bytes);
661 return -EINVAL;
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800662 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700663
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800664 sgl_count = iov_iter_npages(iter, 0xffff);
665 if (sgl_count > max_sgls) {
666 pr_err("%s: requested sgl_count: %d exceeds pre-allocated"
667 " max_sgls: %d\n", __func__, sgl_count, max_sgls);
668 return -EINVAL;
669 }
670 return sgl_count;
671}
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700672
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800673static int
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800674vhost_scsi_iov_to_sgl(struct vhost_scsi_cmd *cmd, bool write,
675 struct iov_iter *iter,
676 struct scatterlist *sg, int sg_count)
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800677{
Al Viro11d49e92017-09-24 18:36:44 -0400678 struct scatterlist *p = sg;
Al Viro2f240c42017-09-24 22:07:59 -0400679 int ret;
Nicholas Bellinger5a01d082014-02-22 18:34:08 -0800680
Al Viro2f240c42017-09-24 22:07:59 -0400681 while (iov_iter_count(iter)) {
682 ret = vhost_scsi_map_to_sgl(cmd, iter, sg, write);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700683 if (ret < 0) {
Al Viro11d49e92017-09-24 18:36:44 -0400684 while (p < sg) {
685 struct page *page = sg_page(p++);
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800686 if (page)
687 put_page(page);
688 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700689 return ret;
690 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700691 sg += ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700692 }
693 return 0;
694}
695
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800696static int
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800697vhost_scsi_mapal(struct vhost_scsi_cmd *cmd,
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800698 size_t prot_bytes, struct iov_iter *prot_iter,
699 size_t data_bytes, struct iov_iter *data_iter)
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800700{
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800701 int sgl_count, ret;
702 bool write = (cmd->tvc_data_direction == DMA_FROM_DEVICE);
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800703
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800704 if (prot_bytes) {
705 sgl_count = vhost_scsi_calc_sgls(prot_iter, prot_bytes,
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800706 VHOST_SCSI_PREALLOC_PROT_SGLS);
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800707 if (sgl_count < 0)
708 return sgl_count;
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800709
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800710 sg_init_table(cmd->tvc_prot_sgl, sgl_count);
711 cmd->tvc_prot_sgl_count = sgl_count;
712 pr_debug("%s prot_sg %p prot_sgl_count %u\n", __func__,
713 cmd->tvc_prot_sgl, cmd->tvc_prot_sgl_count);
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800714
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800715 ret = vhost_scsi_iov_to_sgl(cmd, write, prot_iter,
716 cmd->tvc_prot_sgl,
717 cmd->tvc_prot_sgl_count);
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800718 if (ret < 0) {
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800719 cmd->tvc_prot_sgl_count = 0;
720 return ret;
721 }
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800722 }
723 sgl_count = vhost_scsi_calc_sgls(data_iter, data_bytes,
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800724 VHOST_SCSI_PREALLOC_SGLS);
Nicholas Bellingere8de56b2015-01-28 00:15:00 -0800725 if (sgl_count < 0)
726 return sgl_count;
727
728 sg_init_table(cmd->tvc_sgl, sgl_count);
729 cmd->tvc_sgl_count = sgl_count;
730 pr_debug("%s data_sg %p data_sgl_count %u\n", __func__,
731 cmd->tvc_sgl, cmd->tvc_sgl_count);
732
733 ret = vhost_scsi_iov_to_sgl(cmd, write, data_iter,
734 cmd->tvc_sgl, cmd->tvc_sgl_count);
735 if (ret < 0) {
736 cmd->tvc_sgl_count = 0;
737 return ret;
Nicholas Bellingere31885d2014-02-22 18:34:43 -0800738 }
739 return 0;
740}
741
Nicholas Bellinger46243862014-12-21 10:42:08 -0800742static int vhost_scsi_to_tcm_attr(int attr)
743{
744 switch (attr) {
745 case VIRTIO_SCSI_S_SIMPLE:
746 return TCM_SIMPLE_TAG;
747 case VIRTIO_SCSI_S_ORDERED:
748 return TCM_ORDERED_TAG;
749 case VIRTIO_SCSI_S_HEAD:
750 return TCM_HEAD_TAG;
751 case VIRTIO_SCSI_S_ACA:
752 return TCM_ACA_TAG;
753 default:
754 break;
755 }
756 return TCM_SIMPLE_TAG;
757}
758
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800759static void vhost_scsi_submission_work(struct work_struct *work)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700760{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -0800761 struct vhost_scsi_cmd *cmd =
762 container_of(work, struct vhost_scsi_cmd, work);
763 struct vhost_scsi_nexus *tv_nexus;
Asias He3c63f662013-05-06 16:38:29 +0800764 struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800765 struct scatterlist *sg_ptr, *sg_prot_ptr = NULL;
766 int rc;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700767
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800768 /* FIXME: BIDI operation */
Asias He3c63f662013-05-06 16:38:29 +0800769 if (cmd->tvc_sgl_count) {
770 sg_ptr = cmd->tvc_sgl;
Nicholas Bellinger95e7c432014-02-22 18:22:31 -0800771
772 if (cmd->tvc_prot_sgl_count)
773 sg_prot_ptr = cmd->tvc_prot_sgl;
774 else
775 se_cmd->prot_pto = true;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700776 } else {
777 sg_ptr = NULL;
778 }
Asias He3c63f662013-05-06 16:38:29 +0800779 tv_nexus = cmd->tvc_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700780
Bart Van Assche649ee052015-04-14 13:26:44 +0200781 se_cmd->tag = 0;
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700782 rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
Asias He3c63f662013-05-06 16:38:29 +0800783 cmd->tvc_cdb, &cmd->tvc_sense_buf[0],
784 cmd->tvc_lun, cmd->tvc_exp_data_len,
Nicholas Bellinger46243862014-12-21 10:42:08 -0800785 vhost_scsi_to_tcm_attr(cmd->tvc_task_attr),
786 cmd->tvc_data_direction, TARGET_SCF_ACK_KREF,
787 sg_ptr, cmd->tvc_sgl_count, NULL, 0, sg_prot_ptr,
788 cmd->tvc_prot_sgl_count);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700789 if (rc < 0) {
790 transport_send_check_condition_and_sense(se_cmd,
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700791 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700792 transport_generic_free_cmd(se_cmd, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700793 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700794}
795
Asias He683bd962013-05-06 16:38:27 +0800796static void
797vhost_scsi_send_bad_target(struct vhost_scsi *vs,
798 struct vhost_virtqueue *vq,
799 int head, unsigned out)
Asias He637ab212013-04-10 15:06:15 +0800800{
801 struct virtio_scsi_cmd_resp __user *resp;
802 struct virtio_scsi_cmd_resp rsp;
803 int ret;
804
805 memset(&rsp, 0, sizeof(rsp));
806 rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
807 resp = vq->iov[out].iov_base;
808 ret = __copy_to_user(resp, &rsp, sizeof(rsp));
809 if (!ret)
810 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
811 else
812 pr_err("Faulted on virtio_scsi_cmd_resp\n");
813}
814
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -0700815static int
816vhost_scsi_get_desc(struct vhost_scsi *vs, struct vhost_virtqueue *vq,
817 struct vhost_scsi_ctx *vc)
818{
819 int ret = -ENXIO;
820
821 vc->head = vhost_get_vq_desc(vq, vq->iov,
822 ARRAY_SIZE(vq->iov), &vc->out, &vc->in,
823 NULL, NULL);
824
825 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
826 vc->head, vc->out, vc->in);
827
828 /* On error, stop handling until the next kick. */
829 if (unlikely(vc->head < 0))
830 goto done;
831
832 /* Nothing new? Wait for eventfd to tell us they refilled. */
833 if (vc->head == vq->num) {
834 if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
835 vhost_disable_notify(&vs->dev, vq);
836 ret = -EAGAIN;
837 }
838 goto done;
839 }
840
841 /*
842 * Get the size of request and response buffers.
Bijan Mottahedeh09d75832018-09-17 17:09:49 -0700843 * FIXME: Not correct for BIDI operation
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -0700844 */
845 vc->out_size = iov_length(vq->iov, vc->out);
846 vc->in_size = iov_length(&vq->iov[vc->out], vc->in);
847
848 /*
849 * Copy over the virtio-scsi request header, which for a
850 * ANY_LAYOUT enabled guest may span multiple iovecs, or a
851 * single iovec may contain both the header + outgoing
852 * WRITE payloads.
853 *
854 * copy_from_iter() will advance out_iter, so that it will
855 * point at the start of the outgoing WRITE payload, if
856 * DMA_TO_DEVICE is set.
857 */
858 iov_iter_init(&vc->out_iter, WRITE, vq->iov, vc->out, vc->out_size);
859 ret = 0;
860
861done:
862 return ret;
863}
864
865static int
866vhost_scsi_chk_size(struct vhost_virtqueue *vq, struct vhost_scsi_ctx *vc)
867{
868 if (unlikely(vc->in_size < vc->rsp_size)) {
869 vq_err(vq,
870 "Response buf too small, need min %zu bytes got %zu",
871 vc->rsp_size, vc->in_size);
872 return -EINVAL;
873 } else if (unlikely(vc->out_size < vc->req_size)) {
874 vq_err(vq,
875 "Request buf too small, need min %zu bytes got %zu",
876 vc->req_size, vc->out_size);
877 return -EIO;
878 }
879
880 return 0;
881}
882
883static int
884vhost_scsi_get_req(struct vhost_virtqueue *vq, struct vhost_scsi_ctx *vc,
885 struct vhost_scsi_tpg **tpgp)
886{
887 int ret = -EIO;
888
889 if (unlikely(!copy_from_iter_full(vc->req, vc->req_size,
Bijan Mottahedeh09d75832018-09-17 17:09:49 -0700890 &vc->out_iter))) {
wangyana691ffb2018-12-13 09:10:14 +0800891 vq_err(vq, "Faulted on copy_from_iter_full\n");
Bijan Mottahedeh09d75832018-09-17 17:09:49 -0700892 } else if (unlikely(*vc->lunp != 1)) {
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -0700893 /* virtio-scsi spec requires byte 0 of the lun to be 1 */
894 vq_err(vq, "Illegal virtio-scsi lun: %u\n", *vc->lunp);
Bijan Mottahedeh09d75832018-09-17 17:09:49 -0700895 } else {
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -0700896 struct vhost_scsi_tpg **vs_tpg, *tpg;
897
Eugenio Pérez247643f2020-03-31 21:27:57 +0200898 vs_tpg = vhost_vq_get_backend(vq); /* validated at handler entry */
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -0700899
900 tpg = READ_ONCE(vs_tpg[*vc->target]);
Bijan Mottahedeh09d75832018-09-17 17:09:49 -0700901 if (unlikely(!tpg)) {
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -0700902 vq_err(vq, "Target 0x%x does not exist\n", *vc->target);
Bijan Mottahedeh09d75832018-09-17 17:09:49 -0700903 } else {
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -0700904 if (tpgp)
905 *tpgp = tpg;
906 ret = 0;
907 }
908 }
909
910 return ret;
911}
912
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -0700913static void
Bijan Mottahedeh09d75832018-09-17 17:09:49 -0700914vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
915{
916 struct vhost_scsi_tpg **vs_tpg, *tpg;
917 struct virtio_scsi_cmd_req v_req;
918 struct virtio_scsi_cmd_req_pi v_req_pi;
919 struct vhost_scsi_ctx vc;
920 struct vhost_scsi_cmd *cmd;
921 struct iov_iter in_iter, prot_iter, data_iter;
922 u64 tag;
923 u32 exp_data_len, data_direction;
Jason Wangc1ea02f2019-05-17 00:29:52 -0400924 int ret, prot_bytes, c = 0;
Bijan Mottahedeh09d75832018-09-17 17:09:49 -0700925 u16 lun;
926 u8 task_attr;
927 bool t10_pi = vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI);
928 void *cdb;
929
930 mutex_lock(&vq->mutex);
931 /*
932 * We can handle the vq only after the endpoint is setup by calling the
933 * VHOST_SCSI_SET_ENDPOINT ioctl.
934 */
Eugenio Pérez247643f2020-03-31 21:27:57 +0200935 vs_tpg = vhost_vq_get_backend(vq);
Bijan Mottahedeh09d75832018-09-17 17:09:49 -0700936 if (!vs_tpg)
937 goto out;
938
939 memset(&vc, 0, sizeof(vc));
940 vc.rsp_size = sizeof(struct virtio_scsi_cmd_resp);
941
942 vhost_disable_notify(&vs->dev, vq);
943
Jason Wangc1ea02f2019-05-17 00:29:52 -0400944 do {
Bijan Mottahedeh09d75832018-09-17 17:09:49 -0700945 ret = vhost_scsi_get_desc(vs, vq, &vc);
946 if (ret)
947 goto err;
948
949 /*
950 * Setup pointers and values based upon different virtio-scsi
951 * request header if T10_PI is enabled in KVM guest.
952 */
953 if (t10_pi) {
954 vc.req = &v_req_pi;
955 vc.req_size = sizeof(v_req_pi);
956 vc.lunp = &v_req_pi.lun[0];
957 vc.target = &v_req_pi.lun[1];
958 } else {
959 vc.req = &v_req;
960 vc.req_size = sizeof(v_req);
961 vc.lunp = &v_req.lun[0];
962 vc.target = &v_req.lun[1];
963 }
964
965 /*
966 * Validate the size of request and response buffers.
967 * Check for a sane response buffer so we can report
968 * early errors back to the guest.
969 */
970 ret = vhost_scsi_chk_size(vq, &vc);
971 if (ret)
972 goto err;
973
974 ret = vhost_scsi_get_req(vq, &vc, &tpg);
975 if (ret)
976 goto err;
977
978 ret = -EIO; /* bad target on any error from here on */
979
980 /*
981 * Determine data_direction by calculating the total outgoing
982 * iovec sizes + incoming iovec sizes vs. virtio-scsi request +
983 * response headers respectively.
984 *
985 * For DMA_TO_DEVICE this is out_iter, which is already pointing
986 * to the right place.
987 *
988 * For DMA_FROM_DEVICE, the iovec will be just past the end
989 * of the virtio-scsi response header in either the same
990 * or immediately following iovec.
991 *
992 * Any associated T10_PI bytes for the outgoing / incoming
993 * payloads are included in calculation of exp_data_len here.
994 */
995 prot_bytes = 0;
996
997 if (vc.out_size > vc.req_size) {
998 data_direction = DMA_TO_DEVICE;
999 exp_data_len = vc.out_size - vc.req_size;
1000 data_iter = vc.out_iter;
1001 } else if (vc.in_size > vc.rsp_size) {
1002 data_direction = DMA_FROM_DEVICE;
1003 exp_data_len = vc.in_size - vc.rsp_size;
1004
1005 iov_iter_init(&in_iter, READ, &vq->iov[vc.out], vc.in,
1006 vc.rsp_size + exp_data_len);
1007 iov_iter_advance(&in_iter, vc.rsp_size);
1008 data_iter = in_iter;
1009 } else {
1010 data_direction = DMA_NONE;
1011 exp_data_len = 0;
1012 }
1013 /*
1014 * If T10_PI header + payload is present, setup prot_iter values
1015 * and recalculate data_iter for vhost_scsi_mapal() mapping to
1016 * host scatterlists via get_user_pages_fast().
1017 */
1018 if (t10_pi) {
1019 if (v_req_pi.pi_bytesout) {
1020 if (data_direction != DMA_TO_DEVICE) {
1021 vq_err(vq, "Received non zero pi_bytesout,"
1022 " but wrong data_direction\n");
1023 goto err;
1024 }
1025 prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesout);
1026 } else if (v_req_pi.pi_bytesin) {
1027 if (data_direction != DMA_FROM_DEVICE) {
1028 vq_err(vq, "Received non zero pi_bytesin,"
1029 " but wrong data_direction\n");
1030 goto err;
1031 }
1032 prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesin);
1033 }
1034 /*
1035 * Set prot_iter to data_iter and truncate it to
1036 * prot_bytes, and advance data_iter past any
1037 * preceeding prot_bytes that may be present.
1038 *
1039 * Also fix up the exp_data_len to reflect only the
1040 * actual data payload length.
1041 */
1042 if (prot_bytes) {
1043 exp_data_len -= prot_bytes;
1044 prot_iter = data_iter;
1045 iov_iter_truncate(&prot_iter, prot_bytes);
1046 iov_iter_advance(&data_iter, prot_bytes);
1047 }
1048 tag = vhost64_to_cpu(vq, v_req_pi.tag);
1049 task_attr = v_req_pi.task_attr;
1050 cdb = &v_req_pi.cdb[0];
1051 lun = ((v_req_pi.lun[2] << 8) | v_req_pi.lun[3]) & 0x3FFF;
1052 } else {
1053 tag = vhost64_to_cpu(vq, v_req.tag);
1054 task_attr = v_req.task_attr;
1055 cdb = &v_req.cdb[0];
1056 lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
1057 }
1058 /*
1059 * Check that the received CDB size does not exceeded our
1060 * hardcoded max for vhost-scsi, then get a pre-allocated
1061 * cmd descriptor for the new virtio-scsi tag.
1062 *
1063 * TODO what if cdb was too small for varlen cdb header?
1064 */
1065 if (unlikely(scsi_command_size(cdb) > VHOST_SCSI_MAX_CDB_SIZE)) {
1066 vq_err(vq, "Received SCSI CDB with command_size: %d that"
1067 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1068 scsi_command_size(cdb), VHOST_SCSI_MAX_CDB_SIZE);
1069 goto err;
1070 }
Mike Christie25b98b62020-11-09 23:33:20 -06001071 cmd = vhost_scsi_get_cmd(vq, tpg, cdb, tag, lun, task_attr,
Bijan Mottahedeh09d75832018-09-17 17:09:49 -07001072 exp_data_len + prot_bytes,
1073 data_direction);
1074 if (IS_ERR(cmd)) {
Mike Christie25b98b62020-11-09 23:33:20 -06001075 vq_err(vq, "vhost_scsi_get_cmd failed %ld\n",
Bijan Mottahedeh09d75832018-09-17 17:09:49 -07001076 PTR_ERR(cmd));
1077 goto err;
1078 }
1079 cmd->tvc_vhost = vs;
1080 cmd->tvc_vq = vq;
1081 cmd->tvc_resp_iov = vq->iov[vc.out];
1082 cmd->tvc_in_iovs = vc.in;
1083
1084 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
1085 cmd->tvc_cdb[0], cmd->tvc_lun);
1086 pr_debug("cmd: %p exp_data_len: %d, prot_bytes: %d data_direction:"
1087 " %d\n", cmd, exp_data_len, prot_bytes, data_direction);
1088
1089 if (data_direction != DMA_NONE) {
1090 if (unlikely(vhost_scsi_mapal(cmd, prot_bytes,
1091 &prot_iter, exp_data_len,
1092 &data_iter))) {
1093 vq_err(vq, "Failed to map iov to sgl\n");
1094 vhost_scsi_release_cmd(&cmd->tvc_se_cmd);
1095 goto err;
1096 }
1097 }
1098 /*
1099 * Save the descriptor from vhost_get_vq_desc() to be used to
1100 * complete the virtio-scsi request in TCM callback context via
1101 * vhost_scsi_queue_data_in() and vhost_scsi_queue_status()
1102 */
1103 cmd->tvc_vq_desc = vc.head;
1104 /*
1105 * Dispatch cmd descriptor for cmwq execution in process
1106 * context provided by vhost_scsi_workqueue. This also ensures
1107 * cmd is executed on the same kworker CPU as this vhost
1108 * thread to gain positive L2 cache locality effects.
1109 */
1110 INIT_WORK(&cmd->work, vhost_scsi_submission_work);
1111 queue_work(vhost_scsi_workqueue, &cmd->work);
1112 ret = 0;
1113err:
1114 /*
1115 * ENXIO: No more requests, or read error, wait for next kick
1116 * EINVAL: Invalid response buffer, drop the request
1117 * EIO: Respond with bad target
1118 * EAGAIN: Pending request
1119 */
1120 if (ret == -ENXIO)
1121 break;
1122 else if (ret == -EIO)
1123 vhost_scsi_send_bad_target(vs, vq, vc.head, vc.out);
Jason Wangc1ea02f2019-05-17 00:29:52 -04001124 } while (likely(!vhost_exceeds_weight(vq, ++c, 0)));
Bijan Mottahedeh09d75832018-09-17 17:09:49 -07001125out:
1126 mutex_unlock(&vq->mutex);
1127}
1128
1129static void
1130vhost_scsi_send_tmf_reject(struct vhost_scsi *vs,
1131 struct vhost_virtqueue *vq,
1132 struct vhost_scsi_ctx *vc)
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001133{
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001134 struct virtio_scsi_ctrl_tmf_resp rsp;
Bijan Mottahedeh8e5dadf2018-12-03 16:48:23 -08001135 struct iov_iter iov_iter;
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001136 int ret;
1137
1138 pr_debug("%s\n", __func__);
1139 memset(&rsp, 0, sizeof(rsp));
1140 rsp.response = VIRTIO_SCSI_S_FUNCTION_REJECTED;
Bijan Mottahedeh8e5dadf2018-12-03 16:48:23 -08001141
1142 iov_iter_init(&iov_iter, READ, &vq->iov[vc->out], vc->in, sizeof(rsp));
1143
1144 ret = copy_to_iter(&rsp, sizeof(rsp), &iov_iter);
1145 if (likely(ret == sizeof(rsp)))
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -07001146 vhost_add_used_and_signal(&vs->dev, vq, vc->head, 0);
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001147 else
1148 pr_err("Faulted on virtio_scsi_ctrl_tmf_resp\n");
1149}
1150
1151static void
1152vhost_scsi_send_an_resp(struct vhost_scsi *vs,
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -07001153 struct vhost_virtqueue *vq,
1154 struct vhost_scsi_ctx *vc)
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001155{
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001156 struct virtio_scsi_ctrl_an_resp rsp;
Bijan Mottahedeh8e5dadf2018-12-03 16:48:23 -08001157 struct iov_iter iov_iter;
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001158 int ret;
1159
1160 pr_debug("%s\n", __func__);
1161 memset(&rsp, 0, sizeof(rsp)); /* event_actual = 0 */
1162 rsp.response = VIRTIO_SCSI_S_OK;
Bijan Mottahedeh8e5dadf2018-12-03 16:48:23 -08001163
1164 iov_iter_init(&iov_iter, READ, &vq->iov[vc->out], vc->in, sizeof(rsp));
1165
1166 ret = copy_to_iter(&rsp, sizeof(rsp), &iov_iter);
1167 if (likely(ret == sizeof(rsp)))
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -07001168 vhost_add_used_and_signal(&vs->dev, vq, vc->head, 0);
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001169 else
1170 pr_err("Faulted on virtio_scsi_ctrl_an_resp\n");
1171}
1172
1173static void
1174vhost_scsi_ctl_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
1175{
1176 union {
1177 __virtio32 type;
1178 struct virtio_scsi_ctrl_an_req an;
1179 struct virtio_scsi_ctrl_tmf_req tmf;
1180 } v_req;
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -07001181 struct vhost_scsi_ctx vc;
1182 size_t typ_size;
Jason Wangc1ea02f2019-05-17 00:29:52 -04001183 int ret, c = 0;
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001184
1185 mutex_lock(&vq->mutex);
1186 /*
1187 * We can handle the vq only after the endpoint is setup by calling the
1188 * VHOST_SCSI_SET_ENDPOINT ioctl.
1189 */
Eugenio Pérez247643f2020-03-31 21:27:57 +02001190 if (!vhost_vq_get_backend(vq))
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001191 goto out;
1192
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -07001193 memset(&vc, 0, sizeof(vc));
1194
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001195 vhost_disable_notify(&vs->dev, vq);
1196
Jason Wangc1ea02f2019-05-17 00:29:52 -04001197 do {
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -07001198 ret = vhost_scsi_get_desc(vs, vq, &vc);
1199 if (ret)
1200 goto err;
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001201
1202 /*
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -07001203 * Get the request type first in order to setup
1204 * other parameters dependent on the type.
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001205 */
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -07001206 vc.req = &v_req.type;
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001207 typ_size = sizeof(v_req.type);
1208
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -07001209 if (unlikely(!copy_from_iter_full(vc.req, typ_size,
1210 &vc.out_iter))) {
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001211 vq_err(vq, "Faulted on copy_from_iter tmf type\n");
1212 /*
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -07001213 * The size of the response buffer depends on the
1214 * request type and must be validated against it.
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001215 * Since the request type is not known, don't send
1216 * a response.
1217 */
1218 continue;
1219 }
1220
Michael S. Tsirkin295c1b92020-07-10 06:36:16 -04001221 switch (vhost32_to_cpu(vq, v_req.type)) {
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001222 case VIRTIO_SCSI_T_TMF:
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -07001223 vc.req = &v_req.tmf;
1224 vc.req_size = sizeof(struct virtio_scsi_ctrl_tmf_req);
1225 vc.rsp_size = sizeof(struct virtio_scsi_ctrl_tmf_resp);
1226 vc.lunp = &v_req.tmf.lun[0];
1227 vc.target = &v_req.tmf.lun[1];
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001228 break;
1229 case VIRTIO_SCSI_T_AN_QUERY:
1230 case VIRTIO_SCSI_T_AN_SUBSCRIBE:
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -07001231 vc.req = &v_req.an;
1232 vc.req_size = sizeof(struct virtio_scsi_ctrl_an_req);
1233 vc.rsp_size = sizeof(struct virtio_scsi_ctrl_an_resp);
1234 vc.lunp = &v_req.an.lun[0];
1235 vc.target = NULL;
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001236 break;
1237 default:
1238 vq_err(vq, "Unknown control request %d", v_req.type);
1239 continue;
1240 }
1241
1242 /*
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -07001243 * Validate the size of request and response buffers.
1244 * Check for a sane response buffer so we can report
1245 * early errors back to the guest.
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001246 */
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -07001247 ret = vhost_scsi_chk_size(vq, &vc);
1248 if (ret)
1249 goto err;
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001250
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -07001251 /*
1252 * Get the rest of the request now that its size is known.
1253 */
1254 vc.req += typ_size;
1255 vc.req_size -= typ_size;
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001256
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -07001257 ret = vhost_scsi_get_req(vq, &vc, NULL);
1258 if (ret)
1259 goto err;
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001260
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -07001261 if (v_req.type == VIRTIO_SCSI_T_TMF)
Bijan Mottahedeh09d75832018-09-17 17:09:49 -07001262 vhost_scsi_send_tmf_reject(vs, vq, &vc);
Bijan Mottahedeh3f8ca2e2018-09-17 17:09:48 -07001263 else
1264 vhost_scsi_send_an_resp(vs, vq, &vc);
1265err:
1266 /*
1267 * ENXIO: No more requests, or read error, wait for next kick
1268 * EINVAL: Invalid response buffer, drop the request
1269 * EIO: Respond with bad target
1270 * EAGAIN: Pending request
1271 */
1272 if (ret == -ENXIO)
1273 break;
1274 else if (ret == -EIO)
1275 vhost_scsi_send_bad_target(vs, vq, vc.head, vc.out);
Jason Wangc1ea02f2019-05-17 00:29:52 -04001276 } while (likely(!vhost_exceeds_weight(vq, ++c, 0)));
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001277out:
1278 mutex_unlock(&vq->mutex);
1279}
1280
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001281static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
1282{
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001283 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1284 poll.work);
1285 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1286
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001287 pr_debug("%s: The handling func for control queue.\n", __func__);
Bijan Mottahedeh0d02dbd2018-09-17 17:09:47 -07001288 vhost_scsi_ctl_handle_vq(vs, vq);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001289}
1290
Asias He683bd962013-05-06 16:38:27 +08001291static void
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001292vhost_scsi_send_evt(struct vhost_scsi *vs,
1293 struct vhost_scsi_tpg *tpg,
Asias He683bd962013-05-06 16:38:27 +08001294 struct se_lun *lun,
1295 u32 event,
1296 u32 reason)
Asias Hea6c9af82013-04-25 15:35:21 +08001297{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001298 struct vhost_scsi_evt *evt;
Asias Hea6c9af82013-04-25 15:35:21 +08001299
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001300 evt = vhost_scsi_allocate_evt(vs, event, reason);
Asias Hea6c9af82013-04-25 15:35:21 +08001301 if (!evt)
1302 return;
1303
1304 if (tpg && lun) {
1305 /* TODO: share lun setup code with virtio-scsi.ko */
1306 /*
1307 * Note: evt->event is zeroed when we allocate it and
1308 * lun[4-7] need to be zero according to virtio-scsi spec.
1309 */
1310 evt->event.lun[0] = 0x01;
Dan Carpenter59c816c2015-02-05 10:37:33 +03001311 evt->event.lun[1] = tpg->tport_tpgt;
Asias Hea6c9af82013-04-25 15:35:21 +08001312 if (lun->unpacked_lun >= 256)
1313 evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
1314 evt->event.lun[3] = lun->unpacked_lun & 0xFF;
1315 }
1316
1317 llist_add(&evt->list, &vs->vs_event_list);
1318 vhost_work_queue(&vs->dev, &vs->vs_event_work);
1319}
1320
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001321static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
1322{
Asias Hea6c9af82013-04-25 15:35:21 +08001323 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1324 poll.work);
1325 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1326
1327 mutex_lock(&vq->mutex);
Eugenio Pérez247643f2020-03-31 21:27:57 +02001328 if (!vhost_vq_get_backend(vq))
Asias Hea6c9af82013-04-25 15:35:21 +08001329 goto out;
1330
1331 if (vs->vs_events_missed)
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001332 vhost_scsi_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
Asias Hea6c9af82013-04-25 15:35:21 +08001333out:
1334 mutex_unlock(&vq->mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001335}
1336
1337static void vhost_scsi_handle_kick(struct vhost_work *work)
1338{
1339 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1340 poll.work);
1341 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1342
Asias He1b7f3902013-02-06 13:20:59 +08001343 vhost_scsi_handle_vq(vs, vq);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001344}
1345
Asias He4f7f46d2013-04-03 14:17:37 +08001346static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
1347{
Asias He3ab2e422013-04-27 11:16:48 +08001348 vhost_poll_flush(&vs->vqs[index].vq.poll);
Asias He4f7f46d2013-04-03 14:17:37 +08001349}
1350
Michael S. Tsirkin3dfbff32013-04-28 15:38:52 +03001351/* Callers must hold dev mutex */
Asias He4f7f46d2013-04-03 14:17:37 +08001352static void vhost_scsi_flush(struct vhost_scsi *vs)
1353{
Asias Hef2f0173d2013-04-27 11:16:49 +08001354 struct vhost_scsi_inflight *old_inflight[VHOST_SCSI_MAX_VQ];
Asias He4f7f46d2013-04-03 14:17:37 +08001355 int i;
1356
Asias Hef2f0173d2013-04-27 11:16:49 +08001357 /* Init new inflight and remember the old inflight */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001358 vhost_scsi_init_inflight(vs, old_inflight);
Asias Hef2f0173d2013-04-27 11:16:49 +08001359
1360 /*
1361 * The inflight->kref was initialized to 1. We decrement it here to
1362 * indicate the start of the flush operation so that it will reach 0
1363 * when all the reqs are finished.
1364 */
1365 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001366 kref_put(&old_inflight[i]->kref, vhost_scsi_done_inflight);
Asias Hef2f0173d2013-04-27 11:16:49 +08001367
1368 /* Flush both the vhost poll and vhost work */
Asias He4f7f46d2013-04-03 14:17:37 +08001369 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1370 vhost_scsi_flush_vq(vs, i);
1371 vhost_work_flush(&vs->dev, &vs->vs_completion_work);
Asias Hea6c9af82013-04-25 15:35:21 +08001372 vhost_work_flush(&vs->dev, &vs->vs_event_work);
Asias Hef2f0173d2013-04-27 11:16:49 +08001373
1374 /* Wait for all reqs issued before the flush to be finished */
1375 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1376 wait_for_completion(&old_inflight[i]->comp);
Asias He4f7f46d2013-04-03 14:17:37 +08001377}
1378
Mike Christie25b98b62020-11-09 23:33:20 -06001379static void vhost_scsi_destroy_vq_cmds(struct vhost_virtqueue *vq)
1380{
1381 struct vhost_scsi_virtqueue *svq = container_of(vq,
1382 struct vhost_scsi_virtqueue, vq);
1383 struct vhost_scsi_cmd *tv_cmd;
1384 unsigned int i;
1385
1386 if (!svq->scsi_cmds)
1387 return;
1388
1389 for (i = 0; i < svq->max_cmds; i++) {
1390 tv_cmd = &svq->scsi_cmds[i];
1391
1392 kfree(tv_cmd->tvc_sgl);
1393 kfree(tv_cmd->tvc_prot_sgl);
1394 kfree(tv_cmd->tvc_upages);
1395 }
1396
1397 sbitmap_free(&svq->scsi_tags);
1398 kfree(svq->scsi_cmds);
1399 svq->scsi_cmds = NULL;
1400}
1401
1402static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds)
1403{
1404 struct vhost_scsi_virtqueue *svq = container_of(vq,
1405 struct vhost_scsi_virtqueue, vq);
1406 struct vhost_scsi_cmd *tv_cmd;
1407 unsigned int i;
1408
1409 if (svq->scsi_cmds)
1410 return 0;
1411
1412 if (sbitmap_init_node(&svq->scsi_tags, max_cmds, -1, GFP_KERNEL,
1413 NUMA_NO_NODE))
1414 return -ENOMEM;
1415 svq->max_cmds = max_cmds;
1416
1417 svq->scsi_cmds = kcalloc(max_cmds, sizeof(*tv_cmd), GFP_KERNEL);
1418 if (!svq->scsi_cmds) {
1419 sbitmap_free(&svq->scsi_tags);
1420 return -ENOMEM;
1421 }
1422
1423 for (i = 0; i < max_cmds; i++) {
1424 tv_cmd = &svq->scsi_cmds[i];
1425
1426 tv_cmd->tvc_sgl = kcalloc(VHOST_SCSI_PREALLOC_SGLS,
1427 sizeof(struct scatterlist),
1428 GFP_KERNEL);
1429 if (!tv_cmd->tvc_sgl) {
1430 pr_err("Unable to allocate tv_cmd->tvc_sgl\n");
1431 goto out;
1432 }
1433
1434 tv_cmd->tvc_upages = kcalloc(VHOST_SCSI_PREALLOC_UPAGES,
1435 sizeof(struct page *),
1436 GFP_KERNEL);
1437 if (!tv_cmd->tvc_upages) {
1438 pr_err("Unable to allocate tv_cmd->tvc_upages\n");
1439 goto out;
1440 }
1441
1442 tv_cmd->tvc_prot_sgl = kcalloc(VHOST_SCSI_PREALLOC_PROT_SGLS,
1443 sizeof(struct scatterlist),
1444 GFP_KERNEL);
1445 if (!tv_cmd->tvc_prot_sgl) {
1446 pr_err("Unable to allocate tv_cmd->tvc_prot_sgl\n");
1447 goto out;
1448 }
1449 }
1450 return 0;
1451out:
1452 vhost_scsi_destroy_vq_cmds(vq);
1453 return -ENOMEM;
1454}
1455
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001456/*
1457 * Called from vhost_scsi_ioctl() context to walk the list of available
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001458 * vhost_scsi_tpg with an active struct vhost_scsi_nexus
Asias Hef2b7daf2013-04-25 15:35:20 +08001459 *
1460 * The lock nesting rule is:
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001461 * vhost_scsi_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001462 */
Asias He683bd962013-05-06 16:38:27 +08001463static int
1464vhost_scsi_set_endpoint(struct vhost_scsi *vs,
1465 struct vhost_scsi_target *t)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001466{
Nicholas Bellingerab8edab2014-10-08 06:19:20 +00001467 struct se_portal_group *se_tpg;
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001468 struct vhost_scsi_tport *tv_tport;
1469 struct vhost_scsi_tpg *tpg;
1470 struct vhost_scsi_tpg **vs_tpg;
Asias He4f7f46d2013-04-03 14:17:37 +08001471 struct vhost_virtqueue *vq;
1472 int index, ret, i, len;
Asias He67e18cf2013-02-05 12:31:57 +08001473 bool match = false;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001474
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001475 mutex_lock(&vhost_scsi_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001476 mutex_lock(&vs->dev.mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001477
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001478 /* Verify that ring has been setup correctly. */
1479 for (index = 0; index < vs->dev.nvqs; ++index) {
1480 /* Verify that ring has been setup correctly. */
Asias He3ab2e422013-04-27 11:16:48 +08001481 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
Asias Hef2b7daf2013-04-25 15:35:20 +08001482 ret = -EFAULT;
1483 goto out;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001484 }
1485 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001486
Asias He4f7f46d2013-04-03 14:17:37 +08001487 len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
1488 vs_tpg = kzalloc(len, GFP_KERNEL);
1489 if (!vs_tpg) {
Asias Hef2b7daf2013-04-25 15:35:20 +08001490 ret = -ENOMEM;
1491 goto out;
Asias He4f7f46d2013-04-03 14:17:37 +08001492 }
1493 if (vs->vs_tpg)
1494 memcpy(vs_tpg, vs->vs_tpg, len);
1495
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001496 list_for_each_entry(tpg, &vhost_scsi_list, tv_tpg_list) {
Asias He98718312013-05-06 16:38:28 +08001497 mutex_lock(&tpg->tv_tpg_mutex);
1498 if (!tpg->tpg_nexus) {
1499 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001500 continue;
1501 }
Asias He98718312013-05-06 16:38:28 +08001502 if (tpg->tv_tpg_vhost_count != 0) {
1503 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001504 continue;
1505 }
Asias He98718312013-05-06 16:38:28 +08001506 tv_tport = tpg->tport;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001507
Asias He67e18cf2013-02-05 12:31:57 +08001508 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
Asias He98718312013-05-06 16:38:28 +08001509 if (vs->vs_tpg && vs->vs_tpg[tpg->tport_tpgt]) {
Asias He98718312013-05-06 16:38:28 +08001510 mutex_unlock(&tpg->tv_tpg_mutex);
Asias Hef2b7daf2013-04-25 15:35:20 +08001511 ret = -EEXIST;
Mike Christie25b98b62020-11-09 23:33:20 -06001512 goto undepend;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001513 }
Nicholas Bellingerab8edab2014-10-08 06:19:20 +00001514 /*
1515 * In order to ensure individual vhost-scsi configfs
1516 * groups cannot be removed while in use by vhost ioctl,
1517 * go ahead and take an explicit se_tpg->tpg_group.cg_item
1518 * dependency now.
1519 */
1520 se_tpg = &tpg->se_tpg;
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001521 ret = target_depend_item(&se_tpg->tpg_group.cg_item);
Nicholas Bellingerab8edab2014-10-08 06:19:20 +00001522 if (ret) {
wangyana691ffb2018-12-13 09:10:14 +08001523 pr_warn("target_depend_item() failed: %d\n", ret);
Nicholas Bellingerab8edab2014-10-08 06:19:20 +00001524 mutex_unlock(&tpg->tv_tpg_mutex);
Mike Christie25b98b62020-11-09 23:33:20 -06001525 goto undepend;
Nicholas Bellingerab8edab2014-10-08 06:19:20 +00001526 }
Asias He98718312013-05-06 16:38:28 +08001527 tpg->tv_tpg_vhost_count++;
1528 tpg->vhost_scsi = vs;
1529 vs_tpg[tpg->tport_tpgt] = tpg;
Asias He67e18cf2013-02-05 12:31:57 +08001530 match = true;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001531 }
Asias He98718312013-05-06 16:38:28 +08001532 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001533 }
Asias He67e18cf2013-02-05 12:31:57 +08001534
1535 if (match) {
1536 memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
1537 sizeof(vs->vs_vhost_wwpn));
Mike Christie25b98b62020-11-09 23:33:20 -06001538
1539 for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) {
1540 vq = &vs->vqs[i].vq;
1541 if (!vhost_vq_is_setup(vq))
1542 continue;
1543
1544 if (vhost_scsi_setup_vq_cmds(vq, vq->num))
1545 goto destroy_vq_cmds;
1546 }
1547
Asias He4f7f46d2013-04-03 14:17:37 +08001548 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
Asias He3ab2e422013-04-27 11:16:48 +08001549 vq = &vs->vqs[i].vq;
Asias He4f7f46d2013-04-03 14:17:37 +08001550 mutex_lock(&vq->mutex);
Eugenio Pérez247643f2020-03-31 21:27:57 +02001551 vhost_vq_set_backend(vq, vs_tpg);
Greg Kurz80f7d032016-02-16 15:59:44 +01001552 vhost_vq_init_access(vq);
Asias He4f7f46d2013-04-03 14:17:37 +08001553 mutex_unlock(&vq->mutex);
1554 }
Asias He67e18cf2013-02-05 12:31:57 +08001555 ret = 0;
1556 } else {
1557 ret = -EEXIST;
1558 }
1559
Asias He4f7f46d2013-04-03 14:17:37 +08001560 /*
1561 * Act as synchronize_rcu to make sure access to
1562 * old vs->vs_tpg is finished.
1563 */
1564 vhost_scsi_flush(vs);
1565 kfree(vs->vs_tpg);
1566 vs->vs_tpg = vs_tpg;
Mike Christie25b98b62020-11-09 23:33:20 -06001567 goto out;
Asias He4f7f46d2013-04-03 14:17:37 +08001568
Mike Christie25b98b62020-11-09 23:33:20 -06001569destroy_vq_cmds:
1570 for (i--; i >= VHOST_SCSI_VQ_IO; i--) {
1571 if (!vhost_vq_get_backend(&vs->vqs[i].vq))
1572 vhost_scsi_destroy_vq_cmds(&vs->vqs[i].vq);
1573 }
1574undepend:
1575 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
1576 tpg = vs_tpg[i];
1577 if (tpg) {
1578 tpg->tv_tpg_vhost_count--;
1579 target_undepend_item(&tpg->se_tpg.tpg_group.cg_item);
1580 }
1581 }
1582 kfree(vs_tpg);
Asias Hef2b7daf2013-04-25 15:35:20 +08001583out:
Asias He67e18cf2013-02-05 12:31:57 +08001584 mutex_unlock(&vs->dev.mutex);
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001585 mutex_unlock(&vhost_scsi_mutex);
Asias He67e18cf2013-02-05 12:31:57 +08001586 return ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001587}
1588
Asias He683bd962013-05-06 16:38:27 +08001589static int
1590vhost_scsi_clear_endpoint(struct vhost_scsi *vs,
1591 struct vhost_scsi_target *t)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001592{
Nicholas Bellingerab8edab2014-10-08 06:19:20 +00001593 struct se_portal_group *se_tpg;
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001594 struct vhost_scsi_tport *tv_tport;
1595 struct vhost_scsi_tpg *tpg;
Asias He4f7f46d2013-04-03 14:17:37 +08001596 struct vhost_virtqueue *vq;
1597 bool match = false;
Asias He67e18cf2013-02-05 12:31:57 +08001598 int index, ret, i;
1599 u8 target;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001600
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001601 mutex_lock(&vhost_scsi_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001602 mutex_lock(&vs->dev.mutex);
1603 /* Verify that ring has been setup correctly. */
1604 for (index = 0; index < vs->dev.nvqs; ++index) {
Asias He3ab2e422013-04-27 11:16:48 +08001605 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001606 ret = -EFAULT;
Asias He038e0af42013-03-15 09:14:05 +08001607 goto err_dev;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001608 }
1609 }
Asias He4f7f46d2013-04-03 14:17:37 +08001610
1611 if (!vs->vs_tpg) {
Asias Hef2b7daf2013-04-25 15:35:20 +08001612 ret = 0;
1613 goto err_dev;
Asias He4f7f46d2013-04-03 14:17:37 +08001614 }
1615
Asias He67e18cf2013-02-05 12:31:57 +08001616 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
1617 target = i;
Asias He98718312013-05-06 16:38:28 +08001618 tpg = vs->vs_tpg[target];
1619 if (!tpg)
Asias He67e18cf2013-02-05 12:31:57 +08001620 continue;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001621
Asias He98718312013-05-06 16:38:28 +08001622 mutex_lock(&tpg->tv_tpg_mutex);
1623 tv_tport = tpg->tport;
Asias He67e18cf2013-02-05 12:31:57 +08001624 if (!tv_tport) {
1625 ret = -ENODEV;
Asias He038e0af42013-03-15 09:14:05 +08001626 goto err_tpg;
Asias He67e18cf2013-02-05 12:31:57 +08001627 }
1628
1629 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
Asias He98718312013-05-06 16:38:28 +08001630 pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu"
Asias He67e18cf2013-02-05 12:31:57 +08001631 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
Asias He98718312013-05-06 16:38:28 +08001632 tv_tport->tport_name, tpg->tport_tpgt,
Asias He67e18cf2013-02-05 12:31:57 +08001633 t->vhost_wwpn, t->vhost_tpgt);
1634 ret = -EINVAL;
Asias He038e0af42013-03-15 09:14:05 +08001635 goto err_tpg;
Asias He67e18cf2013-02-05 12:31:57 +08001636 }
Asias He98718312013-05-06 16:38:28 +08001637 tpg->tv_tpg_vhost_count--;
1638 tpg->vhost_scsi = NULL;
Asias He67e18cf2013-02-05 12:31:57 +08001639 vs->vs_tpg[target] = NULL;
Asias He4f7f46d2013-04-03 14:17:37 +08001640 match = true;
Asias He98718312013-05-06 16:38:28 +08001641 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellingerab8edab2014-10-08 06:19:20 +00001642 /*
1643 * Release se_tpg->tpg_group.cg_item configfs dependency now
1644 * to allow vhost-scsi WWPN se_tpg->tpg_group shutdown to occur.
1645 */
1646 se_tpg = &tpg->se_tpg;
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001647 target_undepend_item(&se_tpg->tpg_group.cg_item);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001648 }
Asias He4f7f46d2013-04-03 14:17:37 +08001649 if (match) {
1650 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
Asias He3ab2e422013-04-27 11:16:48 +08001651 vq = &vs->vqs[i].vq;
Asias He4f7f46d2013-04-03 14:17:37 +08001652 mutex_lock(&vq->mutex);
Eugenio Pérez247643f2020-03-31 21:27:57 +02001653 vhost_vq_set_backend(vq, NULL);
Asias He4f7f46d2013-04-03 14:17:37 +08001654 mutex_unlock(&vq->mutex);
Mike Christie25b98b62020-11-09 23:33:20 -06001655 /*
1656 * Make sure cmds are not running before tearing them
1657 * down.
1658 */
1659 vhost_scsi_flush(vs);
1660 vhost_scsi_destroy_vq_cmds(vq);
Asias He4f7f46d2013-04-03 14:17:37 +08001661 }
1662 }
1663 /*
1664 * Act as synchronize_rcu to make sure access to
1665 * old vs->vs_tpg is finished.
1666 */
1667 vhost_scsi_flush(vs);
1668 kfree(vs->vs_tpg);
1669 vs->vs_tpg = NULL;
Asias Hea6c9af82013-04-25 15:35:21 +08001670 WARN_ON(vs->vs_events_nr);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001671 mutex_unlock(&vs->dev.mutex);
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001672 mutex_unlock(&vhost_scsi_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001673 return 0;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001674
Asias He038e0af42013-03-15 09:14:05 +08001675err_tpg:
Asias He98718312013-05-06 16:38:28 +08001676 mutex_unlock(&tpg->tv_tpg_mutex);
Asias He038e0af42013-03-15 09:14:05 +08001677err_dev:
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001678 mutex_unlock(&vs->dev.mutex);
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001679 mutex_unlock(&vhost_scsi_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001680 return ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001681}
1682
Asias He4f7f46d2013-04-03 14:17:37 +08001683static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
1684{
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001685 struct vhost_virtqueue *vq;
1686 int i;
1687
Asias He4f7f46d2013-04-03 14:17:37 +08001688 if (features & ~VHOST_SCSI_FEATURES)
1689 return -EOPNOTSUPP;
1690
1691 mutex_lock(&vs->dev.mutex);
1692 if ((features & (1 << VHOST_F_LOG_ALL)) &&
1693 !vhost_log_access_ok(&vs->dev)) {
1694 mutex_unlock(&vs->dev.mutex);
1695 return -EFAULT;
1696 }
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001697
1698 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1699 vq = &vs->vqs[i].vq;
1700 mutex_lock(&vq->mutex);
1701 vq->acked_features = features;
1702 mutex_unlock(&vq->mutex);
1703 }
Asias He4f7f46d2013-04-03 14:17:37 +08001704 mutex_unlock(&vs->dev.mutex);
1705 return 0;
1706}
1707
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001708static int vhost_scsi_open(struct inode *inode, struct file *f)
1709{
Asias Hec7289312013-05-06 16:38:26 +08001710 struct vhost_scsi *vs;
Asias He3ab2e422013-04-27 11:16:48 +08001711 struct vhost_virtqueue **vqs;
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001712 int r = -ENOMEM, i;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001713
Michal Hockodcda9b02017-07-12 14:36:45 -07001714 vs = kzalloc(sizeof(*vs), GFP_KERNEL | __GFP_NOWARN | __GFP_RETRY_MAYFAIL);
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001715 if (!vs) {
1716 vs = vzalloc(sizeof(*vs));
1717 if (!vs)
1718 goto err_vs;
1719 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001720
Kees Cook6da2ec52018-06-12 13:55:00 -07001721 vqs = kmalloc_array(VHOST_SCSI_MAX_VQ, sizeof(*vqs), GFP_KERNEL);
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001722 if (!vqs)
1723 goto err_vqs;
Asias He3ab2e422013-04-27 11:16:48 +08001724
Asias Hec7289312013-05-06 16:38:26 +08001725 vhost_work_init(&vs->vs_completion_work, vhost_scsi_complete_cmd_work);
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001726 vhost_work_init(&vs->vs_event_work, vhost_scsi_evt_work);
Asias Hea6c9af82013-04-25 15:35:21 +08001727
Asias Hec7289312013-05-06 16:38:26 +08001728 vs->vs_events_nr = 0;
1729 vs->vs_events_missed = false;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001730
Asias Hec7289312013-05-06 16:38:26 +08001731 vqs[VHOST_SCSI_VQ_CTL] = &vs->vqs[VHOST_SCSI_VQ_CTL].vq;
1732 vqs[VHOST_SCSI_VQ_EVT] = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1733 vs->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick;
1734 vs->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick;
Asias He3ab2e422013-04-27 11:16:48 +08001735 for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) {
Asias Hec7289312013-05-06 16:38:26 +08001736 vqs[i] = &vs->vqs[i].vq;
1737 vs->vqs[i].vq.handle_kick = vhost_scsi_handle_kick;
Asias He3ab2e422013-04-27 11:16:48 +08001738 }
Jason Wange82b9b02019-05-17 00:29:49 -04001739 vhost_dev_init(&vs->dev, vqs, VHOST_SCSI_MAX_VQ, UIO_MAXIOV,
Jason Wang01fcb1c2020-05-29 16:02:58 +08001740 VHOST_SCSI_WEIGHT, 0, true, NULL);
Asias Hef2f0173d2013-04-27 11:16:49 +08001741
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001742 vhost_scsi_init_inflight(vs, NULL);
Asias Hef2f0173d2013-04-27 11:16:49 +08001743
Asias Hec7289312013-05-06 16:38:26 +08001744 f->private_data = vs;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001745 return 0;
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001746
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001747err_vqs:
Michael S. Tsirkin68404442014-06-12 19:00:01 +03001748 kvfree(vs);
Michael S. Tsirkin595cb752013-09-17 09:30:34 +03001749err_vs:
1750 return r;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001751}
1752
1753static int vhost_scsi_release(struct inode *inode, struct file *f)
1754{
Asias Hec7289312013-05-06 16:38:26 +08001755 struct vhost_scsi *vs = f->private_data;
Asias He67e18cf2013-02-05 12:31:57 +08001756 struct vhost_scsi_target t;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001757
Asias Hec7289312013-05-06 16:38:26 +08001758 mutex_lock(&vs->dev.mutex);
1759 memcpy(t.vhost_wwpn, vs->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
1760 mutex_unlock(&vs->dev.mutex);
1761 vhost_scsi_clear_endpoint(vs, &t);
1762 vhost_dev_stop(&vs->dev);
夷则(Caspar)f6f93f72017-12-25 00:08:58 +08001763 vhost_dev_cleanup(&vs->dev);
Asias Hea6c9af82013-04-25 15:35:21 +08001764 /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
Asias Hec7289312013-05-06 16:38:26 +08001765 vhost_scsi_flush(vs);
1766 kfree(vs->dev.vqs);
Michael S. Tsirkin68404442014-06-12 19:00:01 +03001767 kvfree(vs);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001768 return 0;
1769}
1770
Asias He683bd962013-05-06 16:38:27 +08001771static long
1772vhost_scsi_ioctl(struct file *f,
1773 unsigned int ioctl,
1774 unsigned long arg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001775{
1776 struct vhost_scsi *vs = f->private_data;
1777 struct vhost_scsi_target backend;
1778 void __user *argp = (void __user *)arg;
1779 u64 __user *featurep = argp;
Asias He11c63412013-04-25 15:35:22 +08001780 u32 __user *eventsp = argp;
1781 u32 events_missed;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001782 u64 features;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001783 int r, abi_version = VHOST_SCSI_ABI_VERSION;
Asias He3ab2e422013-04-27 11:16:48 +08001784 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001785
1786 switch (ioctl) {
1787 case VHOST_SCSI_SET_ENDPOINT:
1788 if (copy_from_user(&backend, argp, sizeof backend))
1789 return -EFAULT;
Michael S. Tsirkin6de71452012-08-18 15:44:09 -07001790 if (backend.reserved != 0)
1791 return -EOPNOTSUPP;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001792
1793 return vhost_scsi_set_endpoint(vs, &backend);
1794 case VHOST_SCSI_CLEAR_ENDPOINT:
1795 if (copy_from_user(&backend, argp, sizeof backend))
1796 return -EFAULT;
Michael S. Tsirkin6de71452012-08-18 15:44:09 -07001797 if (backend.reserved != 0)
1798 return -EOPNOTSUPP;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001799
1800 return vhost_scsi_clear_endpoint(vs, &backend);
1801 case VHOST_SCSI_GET_ABI_VERSION:
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001802 if (copy_to_user(argp, &abi_version, sizeof abi_version))
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001803 return -EFAULT;
1804 return 0;
Asias He11c63412013-04-25 15:35:22 +08001805 case VHOST_SCSI_SET_EVENTS_MISSED:
1806 if (get_user(events_missed, eventsp))
1807 return -EFAULT;
1808 mutex_lock(&vq->mutex);
1809 vs->vs_events_missed = events_missed;
1810 mutex_unlock(&vq->mutex);
1811 return 0;
1812 case VHOST_SCSI_GET_EVENTS_MISSED:
1813 mutex_lock(&vq->mutex);
1814 events_missed = vs->vs_events_missed;
1815 mutex_unlock(&vq->mutex);
1816 if (put_user(events_missed, eventsp))
1817 return -EFAULT;
1818 return 0;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001819 case VHOST_GET_FEATURES:
Nicholas Bellinger5dade712013-03-27 17:23:41 -07001820 features = VHOST_SCSI_FEATURES;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001821 if (copy_to_user(featurep, &features, sizeof features))
1822 return -EFAULT;
1823 return 0;
1824 case VHOST_SET_FEATURES:
1825 if (copy_from_user(&features, featurep, sizeof features))
1826 return -EFAULT;
1827 return vhost_scsi_set_features(vs, features);
1828 default:
1829 mutex_lock(&vs->dev.mutex);
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001830 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
1831 /* TODO: flush backend after dev ioctl. */
1832 if (r == -ENOIOCTLCMD)
1833 r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001834 mutex_unlock(&vs->dev.mutex);
1835 return r;
1836 }
1837}
1838
1839static const struct file_operations vhost_scsi_fops = {
1840 .owner = THIS_MODULE,
1841 .release = vhost_scsi_release,
1842 .unlocked_ioctl = vhost_scsi_ioctl,
Arnd Bergmann407e9ef2018-09-11 17:23:00 +02001843 .compat_ioctl = compat_ptr_ioctl,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001844 .open = vhost_scsi_open,
1845 .llseek = noop_llseek,
1846};
1847
1848static struct miscdevice vhost_scsi_misc = {
1849 MISC_DYNAMIC_MINOR,
1850 "vhost-scsi",
1851 &vhost_scsi_fops,
1852};
1853
1854static int __init vhost_scsi_register(void)
1855{
1856 return misc_register(&vhost_scsi_misc);
1857}
1858
Greg Kroah-Hartmanf368ed62015-07-30 15:59:57 -07001859static void vhost_scsi_deregister(void)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001860{
Greg Kroah-Hartmanf368ed62015-07-30 15:59:57 -07001861 misc_deregister(&vhost_scsi_misc);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001862}
1863
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001864static char *vhost_scsi_dump_proto_id(struct vhost_scsi_tport *tport)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001865{
1866 switch (tport->tport_proto_id) {
1867 case SCSI_PROTOCOL_SAS:
1868 return "SAS";
1869 case SCSI_PROTOCOL_FCP:
1870 return "FCP";
1871 case SCSI_PROTOCOL_ISCSI:
1872 return "iSCSI";
1873 default:
1874 break;
1875 }
1876
1877 return "Unknown";
1878}
1879
Asias He683bd962013-05-06 16:38:27 +08001880static void
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001881vhost_scsi_do_plug(struct vhost_scsi_tpg *tpg,
Asias He683bd962013-05-06 16:38:27 +08001882 struct se_lun *lun, bool plug)
Asias Hea6c9af82013-04-25 15:35:21 +08001883{
1884
1885 struct vhost_scsi *vs = tpg->vhost_scsi;
1886 struct vhost_virtqueue *vq;
1887 u32 reason;
1888
1889 if (!vs)
1890 return;
1891
1892 mutex_lock(&vs->dev.mutex);
Asias Hea6c9af82013-04-25 15:35:21 +08001893
1894 if (plug)
1895 reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
1896 else
1897 reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
1898
Asias He3ab2e422013-04-27 11:16:48 +08001899 vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
Asias Hea6c9af82013-04-25 15:35:21 +08001900 mutex_lock(&vq->mutex);
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001901 if (vhost_has_feature(vq, VIRTIO_SCSI_F_HOTPLUG))
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001902 vhost_scsi_send_evt(vs, tpg, lun,
Michael S. Tsirkinea16c512014-06-05 15:20:23 +03001903 VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
Asias Hea6c9af82013-04-25 15:35:21 +08001904 mutex_unlock(&vq->mutex);
1905 mutex_unlock(&vs->dev.mutex);
1906}
1907
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001908static void vhost_scsi_hotplug(struct vhost_scsi_tpg *tpg, struct se_lun *lun)
Asias Hea6c9af82013-04-25 15:35:21 +08001909{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001910 vhost_scsi_do_plug(tpg, lun, true);
Asias Hea6c9af82013-04-25 15:35:21 +08001911}
1912
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001913static void vhost_scsi_hotunplug(struct vhost_scsi_tpg *tpg, struct se_lun *lun)
Asias Hea6c9af82013-04-25 15:35:21 +08001914{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001915 vhost_scsi_do_plug(tpg, lun, false);
Asias Hea6c9af82013-04-25 15:35:21 +08001916}
1917
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001918static int vhost_scsi_port_link(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08001919 struct se_lun *lun)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001920{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001921 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
1922 struct vhost_scsi_tpg, se_tpg);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001923
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001924 mutex_lock(&vhost_scsi_mutex);
Asias Hea6c9af82013-04-25 15:35:21 +08001925
Asias He98718312013-05-06 16:38:28 +08001926 mutex_lock(&tpg->tv_tpg_mutex);
1927 tpg->tv_tpg_port_count++;
1928 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001929
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001930 vhost_scsi_hotplug(tpg, lun);
Asias Hea6c9af82013-04-25 15:35:21 +08001931
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001932 mutex_unlock(&vhost_scsi_mutex);
Asias Hea6c9af82013-04-25 15:35:21 +08001933
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001934 return 0;
1935}
1936
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001937static void vhost_scsi_port_unlink(struct se_portal_group *se_tpg,
Asias He683bd962013-05-06 16:38:27 +08001938 struct se_lun *lun)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001939{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001940 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
1941 struct vhost_scsi_tpg, se_tpg);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001942
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001943 mutex_lock(&vhost_scsi_mutex);
Asias Hea6c9af82013-04-25 15:35:21 +08001944
Asias He98718312013-05-06 16:38:28 +08001945 mutex_lock(&tpg->tv_tpg_mutex);
1946 tpg->tv_tpg_port_count--;
1947 mutex_unlock(&tpg->tv_tpg_mutex);
Asias Hea6c9af82013-04-25 15:35:21 +08001948
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001949 vhost_scsi_hotunplug(tpg, lun);
Asias Hea6c9af82013-04-25 15:35:21 +08001950
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001951 mutex_unlock(&vhost_scsi_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001952}
1953
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001954static ssize_t vhost_scsi_tpg_attrib_fabric_prot_type_store(
1955 struct config_item *item, const char *page, size_t count)
Nicholas Bellingerb1d75fe2015-03-28 00:03:51 -07001956{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001957 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Nicholas Bellingerb1d75fe2015-03-28 00:03:51 -07001958 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
1959 struct vhost_scsi_tpg, se_tpg);
1960 unsigned long val;
1961 int ret = kstrtoul(page, 0, &val);
1962
1963 if (ret) {
1964 pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
1965 return ret;
1966 }
1967 if (val != 0 && val != 1 && val != 3) {
1968 pr_err("Invalid vhost_scsi fabric_prot_type: %lu\n", val);
1969 return -EINVAL;
1970 }
1971 tpg->tv_fabric_prot_type = val;
1972
1973 return count;
1974}
1975
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001976static ssize_t vhost_scsi_tpg_attrib_fabric_prot_type_show(
1977 struct config_item *item, char *page)
Nicholas Bellingerb1d75fe2015-03-28 00:03:51 -07001978{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001979 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Nicholas Bellingerb1d75fe2015-03-28 00:03:51 -07001980 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
1981 struct vhost_scsi_tpg, se_tpg);
1982
1983 return sprintf(page, "%d\n", tpg->tv_fabric_prot_type);
1984}
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001985
1986CONFIGFS_ATTR(vhost_scsi_tpg_attrib_, fabric_prot_type);
Nicholas Bellingerb1d75fe2015-03-28 00:03:51 -07001987
1988static struct configfs_attribute *vhost_scsi_tpg_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001989 &vhost_scsi_tpg_attrib_attr_fabric_prot_type,
Nicholas Bellingerb1d75fe2015-03-28 00:03:51 -07001990 NULL,
1991};
1992
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001993static int vhost_scsi_make_nexus(struct vhost_scsi_tpg *tpg,
Asias He683bd962013-05-06 16:38:27 +08001994 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001995{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08001996 struct vhost_scsi_nexus *tv_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001997
Asias He98718312013-05-06 16:38:28 +08001998 mutex_lock(&tpg->tv_tpg_mutex);
1999 if (tpg->tpg_nexus) {
2000 mutex_unlock(&tpg->tv_tpg_mutex);
2001 pr_debug("tpg->tpg_nexus already exists\n");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002002 return -EEXIST;
2003 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002004
Markus Elfring473f0b12017-05-20 13:48:44 +02002005 tv_nexus = kzalloc(sizeof(*tv_nexus), GFP_KERNEL);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002006 if (!tv_nexus) {
Asias He98718312013-05-06 16:38:28 +08002007 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002008 pr_err("Unable to allocate struct vhost_scsi_nexus\n");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002009 return -ENOMEM;
2010 }
2011 /*
Nicholas Bellinger65ea7892016-01-09 05:47:55 -08002012 * Since we are running in 'demo mode' this call with generate a
2013 * struct se_node_acl for the vhost_scsi struct se_portal_group with
2014 * the SCSI Initiator port name of the passed configfs group 'name'.
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002015 */
Mike Christie25b98b62020-11-09 23:33:20 -06002016 tv_nexus->tvn_se_sess = target_setup_session(&tpg->se_tpg, 0, 0,
Nicholas Bellinger65ea7892016-01-09 05:47:55 -08002017 TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS,
Mike Christie25b98b62020-11-09 23:33:20 -06002018 (unsigned char *)name, tv_nexus, NULL);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002019 if (IS_ERR(tv_nexus->tvn_se_sess)) {
Asias He98718312013-05-06 16:38:28 +08002020 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002021 kfree(tv_nexus);
2022 return -ENOMEM;
2023 }
Asias He98718312013-05-06 16:38:28 +08002024 tpg->tpg_nexus = tv_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002025
Asias He98718312013-05-06 16:38:28 +08002026 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002027 return 0;
2028}
2029
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002030static int vhost_scsi_drop_nexus(struct vhost_scsi_tpg *tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002031{
2032 struct se_session *se_sess;
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002033 struct vhost_scsi_nexus *tv_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002034
2035 mutex_lock(&tpg->tv_tpg_mutex);
2036 tv_nexus = tpg->tpg_nexus;
2037 if (!tv_nexus) {
2038 mutex_unlock(&tpg->tv_tpg_mutex);
2039 return -ENODEV;
2040 }
2041
2042 se_sess = tv_nexus->tvn_se_sess;
2043 if (!se_sess) {
2044 mutex_unlock(&tpg->tv_tpg_mutex);
2045 return -ENODEV;
2046 }
2047
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002048 if (tpg->tv_tpg_port_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002049 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002050 pr_err("Unable to remove TCM_vhost I_T Nexus with"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002051 " active TPG port count: %d\n",
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002052 tpg->tv_tpg_port_count);
2053 return -EBUSY;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002054 }
2055
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002056 if (tpg->tv_tpg_vhost_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002057 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002058 pr_err("Unable to remove TCM_vhost I_T Nexus with"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002059 " active TPG vhost count: %d\n",
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002060 tpg->tv_tpg_vhost_count);
2061 return -EBUSY;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002062 }
2063
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002064 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002065 " %s Initiator Port: %s\n", vhost_scsi_dump_proto_id(tpg->tport),
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002066 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
Nicholas Bellinger3aee26b2013-06-21 14:32:04 -07002067
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002068 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002069 * Release the SCSI I_T Nexus to the emulated vhost Target Port
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002070 */
Mike Christie25b88552018-08-02 12:12:27 -05002071 target_remove_session(se_sess);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002072 tpg->tpg_nexus = NULL;
2073 mutex_unlock(&tpg->tv_tpg_mutex);
2074
2075 kfree(tv_nexus);
2076 return 0;
2077}
2078
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002079static ssize_t vhost_scsi_tpg_nexus_show(struct config_item *item, char *page)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002080{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002081 struct se_portal_group *se_tpg = to_tpg(item);
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002082 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
2083 struct vhost_scsi_tpg, se_tpg);
2084 struct vhost_scsi_nexus *tv_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002085 ssize_t ret;
2086
Asias He98718312013-05-06 16:38:28 +08002087 mutex_lock(&tpg->tv_tpg_mutex);
2088 tv_nexus = tpg->tpg_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002089 if (!tv_nexus) {
Asias He98718312013-05-06 16:38:28 +08002090 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002091 return -ENODEV;
2092 }
2093 ret = snprintf(page, PAGE_SIZE, "%s\n",
2094 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
Asias He98718312013-05-06 16:38:28 +08002095 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002096
2097 return ret;
2098}
2099
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002100static ssize_t vhost_scsi_tpg_nexus_store(struct config_item *item,
2101 const char *page, size_t count)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002102{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002103 struct se_portal_group *se_tpg = to_tpg(item);
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002104 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
2105 struct vhost_scsi_tpg, se_tpg);
2106 struct vhost_scsi_tport *tport_wwn = tpg->tport;
2107 unsigned char i_port[VHOST_SCSI_NAMELEN], *ptr, *port_ptr;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002108 int ret;
2109 /*
2110 * Shutdown the active I_T nexus if 'NULL' is passed..
2111 */
2112 if (!strncmp(page, "NULL", 4)) {
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002113 ret = vhost_scsi_drop_nexus(tpg);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002114 return (!ret) ? count : ret;
2115 }
2116 /*
2117 * Otherwise make sure the passed virtual Initiator port WWN matches
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002118 * the fabric protocol_id set in vhost_scsi_make_tport(), and call
2119 * vhost_scsi_make_nexus().
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002120 */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002121 if (strlen(page) >= VHOST_SCSI_NAMELEN) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002122 pr_err("Emulated NAA Sas Address: %s, exceeds"
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002123 " max: %d\n", page, VHOST_SCSI_NAMELEN);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002124 return -EINVAL;
2125 }
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002126 snprintf(&i_port[0], VHOST_SCSI_NAMELEN, "%s", page);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002127
2128 ptr = strstr(i_port, "naa.");
2129 if (ptr) {
2130 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
2131 pr_err("Passed SAS Initiator Port %s does not"
2132 " match target port protoid: %s\n", i_port,
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002133 vhost_scsi_dump_proto_id(tport_wwn));
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002134 return -EINVAL;
2135 }
2136 port_ptr = &i_port[0];
2137 goto check_newline;
2138 }
2139 ptr = strstr(i_port, "fc.");
2140 if (ptr) {
2141 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
2142 pr_err("Passed FCP Initiator Port %s does not"
2143 " match target port protoid: %s\n", i_port,
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002144 vhost_scsi_dump_proto_id(tport_wwn));
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002145 return -EINVAL;
2146 }
2147 port_ptr = &i_port[3]; /* Skip over "fc." */
2148 goto check_newline;
2149 }
2150 ptr = strstr(i_port, "iqn.");
2151 if (ptr) {
2152 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
2153 pr_err("Passed iSCSI Initiator Port %s does not"
2154 " match target port protoid: %s\n", i_port,
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002155 vhost_scsi_dump_proto_id(tport_wwn));
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002156 return -EINVAL;
2157 }
2158 port_ptr = &i_port[0];
2159 goto check_newline;
2160 }
2161 pr_err("Unable to locate prefix for emulated Initiator Port:"
2162 " %s\n", i_port);
2163 return -EINVAL;
2164 /*
2165 * Clear any trailing newline for the NAA WWN
2166 */
2167check_newline:
2168 if (i_port[strlen(i_port)-1] == '\n')
2169 i_port[strlen(i_port)-1] = '\0';
2170
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002171 ret = vhost_scsi_make_nexus(tpg, port_ptr);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002172 if (ret < 0)
2173 return ret;
2174
2175 return count;
2176}
2177
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002178CONFIGFS_ATTR(vhost_scsi_tpg_, nexus);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002179
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002180static struct configfs_attribute *vhost_scsi_tpg_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002181 &vhost_scsi_tpg_attr_nexus,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002182 NULL,
2183};
2184
Asias He683bd962013-05-06 16:38:27 +08002185static struct se_portal_group *
Bart Van Asscheaa090ea2018-06-22 14:53:02 -07002186vhost_scsi_make_tpg(struct se_wwn *wwn, const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002187{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002188 struct vhost_scsi_tport *tport = container_of(wwn,
2189 struct vhost_scsi_tport, tport_wwn);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002190
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002191 struct vhost_scsi_tpg *tpg;
Dan Carpenter59c816c2015-02-05 10:37:33 +03002192 u16 tpgt;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002193 int ret;
2194
2195 if (strstr(name, "tpgt_") != name)
2196 return ERR_PTR(-EINVAL);
Dan Carpenter59c816c2015-02-05 10:37:33 +03002197 if (kstrtou16(name + 5, 10, &tpgt) || tpgt >= VHOST_SCSI_MAX_TARGET)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002198 return ERR_PTR(-EINVAL);
2199
Markus Elfring473f0b12017-05-20 13:48:44 +02002200 tpg = kzalloc(sizeof(*tpg), GFP_KERNEL);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002201 if (!tpg) {
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002202 pr_err("Unable to allocate struct vhost_scsi_tpg");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002203 return ERR_PTR(-ENOMEM);
2204 }
2205 mutex_init(&tpg->tv_tpg_mutex);
2206 INIT_LIST_HEAD(&tpg->tv_tpg_list);
2207 tpg->tport = tport;
2208 tpg->tport_tpgt = tpgt;
2209
Nicholas Bellingerbc0c94b2015-05-20 21:48:03 -07002210 ret = core_tpg_register(wwn, &tpg->se_tpg, tport->tport_proto_id);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002211 if (ret < 0) {
2212 kfree(tpg);
2213 return NULL;
2214 }
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002215 mutex_lock(&vhost_scsi_mutex);
2216 list_add_tail(&tpg->tv_tpg_list, &vhost_scsi_list);
2217 mutex_unlock(&vhost_scsi_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002218
2219 return &tpg->se_tpg;
2220}
2221
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002222static void vhost_scsi_drop_tpg(struct se_portal_group *se_tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002223{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002224 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
2225 struct vhost_scsi_tpg, se_tpg);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002226
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002227 mutex_lock(&vhost_scsi_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002228 list_del(&tpg->tv_tpg_list);
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002229 mutex_unlock(&vhost_scsi_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002230 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002231 * Release the virtual I_T Nexus for this vhost TPG
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002232 */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002233 vhost_scsi_drop_nexus(tpg);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002234 /*
2235 * Deregister the se_tpg from TCM..
2236 */
2237 core_tpg_deregister(se_tpg);
2238 kfree(tpg);
2239}
2240
Asias He683bd962013-05-06 16:38:27 +08002241static struct se_wwn *
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002242vhost_scsi_make_tport(struct target_fabric_configfs *tf,
Asias He683bd962013-05-06 16:38:27 +08002243 struct config_group *group,
2244 const char *name)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002245{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002246 struct vhost_scsi_tport *tport;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002247 char *ptr;
2248 u64 wwpn = 0;
2249 int off = 0;
2250
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002251 /* if (vhost_scsi_parse_wwn(name, &wwpn, 1) < 0)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002252 return ERR_PTR(-EINVAL); */
2253
Markus Elfring473f0b12017-05-20 13:48:44 +02002254 tport = kzalloc(sizeof(*tport), GFP_KERNEL);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002255 if (!tport) {
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002256 pr_err("Unable to allocate struct vhost_scsi_tport");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002257 return ERR_PTR(-ENOMEM);
2258 }
2259 tport->tport_wwpn = wwpn;
2260 /*
2261 * Determine the emulated Protocol Identifier and Target Port Name
2262 * based on the incoming configfs directory name.
2263 */
2264 ptr = strstr(name, "naa.");
2265 if (ptr) {
2266 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
2267 goto check_len;
2268 }
2269 ptr = strstr(name, "fc.");
2270 if (ptr) {
2271 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
2272 off = 3; /* Skip over "fc." */
2273 goto check_len;
2274 }
2275 ptr = strstr(name, "iqn.");
2276 if (ptr) {
2277 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
2278 goto check_len;
2279 }
2280
2281 pr_err("Unable to locate prefix for emulated Target Port:"
2282 " %s\n", name);
2283 kfree(tport);
2284 return ERR_PTR(-EINVAL);
2285
2286check_len:
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002287 if (strlen(name) >= VHOST_SCSI_NAMELEN) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002288 pr_err("Emulated %s Address: %s, exceeds"
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002289 " max: %d\n", name, vhost_scsi_dump_proto_id(tport),
2290 VHOST_SCSI_NAMELEN);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002291 kfree(tport);
2292 return ERR_PTR(-EINVAL);
2293 }
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002294 snprintf(&tport->tport_name[0], VHOST_SCSI_NAMELEN, "%s", &name[off]);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002295
2296 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002297 " %s Address: %s\n", vhost_scsi_dump_proto_id(tport), name);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002298
2299 return &tport->tport_wwn;
2300}
2301
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002302static void vhost_scsi_drop_tport(struct se_wwn *wwn)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002303{
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002304 struct vhost_scsi_tport *tport = container_of(wwn,
2305 struct vhost_scsi_tport, tport_wwn);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002306
2307 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002308 " %s Address: %s\n", vhost_scsi_dump_proto_id(tport),
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002309 tport->tport_name);
2310
2311 kfree(tport);
2312}
2313
Asias He683bd962013-05-06 16:38:27 +08002314static ssize_t
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002315vhost_scsi_wwn_version_show(struct config_item *item, char *page)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002316{
2317 return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002318 "on "UTS_RELEASE"\n", VHOST_SCSI_VERSION, utsname()->sysname,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002319 utsname()->machine);
2320}
2321
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002322CONFIGFS_ATTR_RO(vhost_scsi_wwn_, version);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002323
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002324static struct configfs_attribute *vhost_scsi_wwn_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002325 &vhost_scsi_wwn_attr_version,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002326 NULL,
2327};
2328
Bhumika Goyal1d822a42017-01-09 20:51:02 +05302329static const struct target_core_fabric_ops vhost_scsi_ops = {
Christoph Hellwig9ac89282015-04-08 20:01:35 +02002330 .module = THIS_MODULE,
David Disseldorp30c7ca92018-11-23 18:36:12 +01002331 .fabric_name = "vhost",
Sudhakar Panneerselvam5ae6a6a2020-05-22 16:51:57 +00002332 .max_data_sg_nents = VHOST_SCSI_PREALLOC_SGLS,
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002333 .tpg_get_wwn = vhost_scsi_get_fabric_wwn,
2334 .tpg_get_tag = vhost_scsi_get_tpgt,
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002335 .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_get_inst_index = vhost_scsi_tpg_get_inst_index,
2341 .release_cmd = vhost_scsi_release_cmd,
Nicholas Bellinger084ed452013-06-06 02:20:41 -07002342 .check_stop_free = vhost_scsi_check_stop_free,
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002343 .sess_get_index = vhost_scsi_sess_get_index,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002344 .sess_get_initiator_sid = NULL,
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002345 .write_pending = vhost_scsi_write_pending,
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002346 .set_default_node_attributes = vhost_scsi_set_default_node_attrs,
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002347 .get_cmd_state = vhost_scsi_get_cmd_state,
2348 .queue_data_in = vhost_scsi_queue_data_in,
2349 .queue_status = vhost_scsi_queue_status,
2350 .queue_tm_rsp = vhost_scsi_queue_tm_rsp,
2351 .aborted_task = vhost_scsi_aborted_task,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002352 /*
2353 * Setup callers for generic logic in target_core_fabric_configfs.c
2354 */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002355 .fabric_make_wwn = vhost_scsi_make_tport,
2356 .fabric_drop_wwn = vhost_scsi_drop_tport,
2357 .fabric_make_tpg = vhost_scsi_make_tpg,
2358 .fabric_drop_tpg = vhost_scsi_drop_tpg,
2359 .fabric_post_link = vhost_scsi_port_link,
2360 .fabric_pre_unlink = vhost_scsi_port_unlink,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002361
Christoph Hellwig9ac89282015-04-08 20:01:35 +02002362 .tfc_wwn_attrs = vhost_scsi_wwn_attrs,
2363 .tfc_tpg_base_attrs = vhost_scsi_tpg_attrs,
2364 .tfc_tpg_attrib_attrs = vhost_scsi_tpg_attrib_attrs,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002365};
2366
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002367static int __init vhost_scsi_init(void)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002368{
2369 int ret = -ENOMEM;
Christoph Hellwig9ac89282015-04-08 20:01:35 +02002370
2371 pr_debug("TCM_VHOST fabric module %s on %s/%s"
2372 " on "UTS_RELEASE"\n", VHOST_SCSI_VERSION, utsname()->sysname,
2373 utsname()->machine);
2374
Nicholas Bellinger101998f2012-07-30 13:30:00 -07002375 /*
2376 * Use our own dedicated workqueue for submitting I/O into
2377 * target core to avoid contention within system_wq.
2378 */
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002379 vhost_scsi_workqueue = alloc_workqueue("vhost_scsi", 0, 0);
2380 if (!vhost_scsi_workqueue)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002381 goto out;
2382
2383 ret = vhost_scsi_register();
2384 if (ret < 0)
2385 goto out_destroy_workqueue;
2386
Christoph Hellwig9ac89282015-04-08 20:01:35 +02002387 ret = target_register_template(&vhost_scsi_ops);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002388 if (ret < 0)
2389 goto out_vhost_scsi_deregister;
2390
2391 return 0;
2392
2393out_vhost_scsi_deregister:
2394 vhost_scsi_deregister();
2395out_destroy_workqueue:
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002396 destroy_workqueue(vhost_scsi_workqueue);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002397out:
2398 return ret;
2399};
2400
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002401static void vhost_scsi_exit(void)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002402{
Christoph Hellwig9ac89282015-04-08 20:01:35 +02002403 target_unregister_template(&vhost_scsi_ops);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002404 vhost_scsi_deregister();
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002405 destroy_workqueue(vhost_scsi_workqueue);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002406};
2407
Michael S. Tsirkin181c04a2013-05-02 03:52:59 +03002408MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
2409MODULE_ALIAS("tcm_vhost");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07002410MODULE_LICENSE("GPL");
Nicholas Bellinger1a1ff822015-01-31 23:56:53 -08002411module_init(vhost_scsi_init);
2412module_exit(vhost_scsi_exit);